Skip to content

Spencer-Sch/FCC_Project_Euler_Solutions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Euler Solutions


Description

My answers for the Project Euler code challenges on FreeCodeCamp.com


Problems

Problem 1 - Multiples of 3 and 5

Question:

Click to expand

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below the provided parameter value number.

My Solution:

Click to expand
function multiplesOf3and5(number) {
  let result = 0;
  for (let i = 0; i < number; i++) {
    if (i % 3 === 0 || i % 5 === 0) {
      result += i;
    }
  }
  return result;
}

multiplesOf3and5(1000);

Problem 2 - Even Fibonacci Numbers

Question:

Click to expand

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the multiples of 3 or 5 below the provided parameter value number.

My Solution:

Click to expand
function fiboEvenSum(n) {
  let n1 = 1;
  let n2 = n1;
  let evenSum = 0;
  for (let i = 0; i < n; i++) {
    let sum = n1 + n2;
    if (sum > n) {
      break;
    } else if (sum % 2 === 0) {
      evenSum += sum;
    }
    n1 = n2;
    n2 = sum;
  }
  return evenSum;
}

fiboEvenSum(10);

Problem 3 - Largest prime factor

Question:

Click to expand

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the given number?

My Solution:

Click to expand
function largestPrimeFactor(number) {
  const primeFactors = [];
  for (let i = 1; i <= number; i++) {
    if (number % i === 0) {
      let facStorage = [];
      for (let j = 1; j <= i; j++) {
        if (i % j === 0) {
          facStorage.push(j);
        }
      }
      if (facStorage.length <= 2) {
        primeFactors.push(i);
      }
    }
  }
  return primeFactors[primeFactors.length - 1];
}

largestPrimeFactor(13195);

Problem 4 - Largest palindrome product

Question:

Click to expand

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two n-digit numbers.

My Solution:

Click to expand
const palindromeFinder = (numStr) => {
  if (numStr.length <= 1) return true;

  if (numStr[0] === numStr[numStr.length - 1]) {
    return palindromeFinder(numStr.slice(1, numStr.length - 1));
  } else return false;
};

const largestPalindromeProduct = (n) => {
  let num = 10 ** n - 1;
  let maxPal = 0;

  for (let i = num; i > 0; i--) {
    for (let j = i; j > 0; j--) {
      let product = j * i;
      if (palindromeFinder(product.toString())) {
        if (product > maxPal) maxPal = product;
      }
    }
  }
  return maxPal;
};

largestPalindromeProduct(2);

Back To The Top

About

My answers to the Project Euler challenges on FreeCodeCamp.com

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published