ProjectEuler.net Solution In Javascript
Last active
September 1, 2020 09:02
-
-
Save defrindr/184a396c8aa26ce35948b917d72276ba to your computer and use it in GitHub Desktop.
ProjectEuler.net Solution In Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Title : Multiples of 3 and 5 | |
// Task : | |
// 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 1000. | |
let sum_of_all = 0; | |
for (i = 0; i < 1000; i++) | |
if (i % 3 == 0 || i % 5 == 0) sum_of_all += i; | |
console.log(sum_of_all); | |
// 233168 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Titlw : Even Fibonacci Number | |
// Task : | |
// 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, ... | |
// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even - valued terms. | |
let [x, y, z] = [0, 1, 0]; | |
let result = 0; | |
while (y < 4000000) { | |
z = x + y; | |
[x, y] = [y, z]; | |
if (y % 2 == 0) result += y; | |
} | |
console.log(result); | |
// 4613732 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Title : Largest Prime Factor | |
// Task : | |
// The prime factors of 13195 are 5, 7, 13 and 29. | |
// What is the largest prime factor of the number 600851475143 ? | |
let number = 600851475143; | |
let primes = []; | |
for (let i = 2; i <= number; i++) { | |
if (number % i === 0) { | |
if (!primes.length) primes.push(i); | |
else { | |
let is_add = true; | |
primes.forEach((value) => { | |
if (i % value === 0) is_add = false; | |
}); | |
if (is_add) primes.push(i); | |
} | |
number /= i; | |
} | |
} | |
console.log(primes[primes.length - 1]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Title : Largest Palindrome Product | |
// Task : | |
// 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 3-digit numbers. | |
let final_result = 0; | |
const floor = (number) => { | |
return number - ( number % 1); | |
}; | |
for(let i = 111; i <= 999; i++){ | |
for (let j = 111; j <= 999; j++) { | |
let result = String(i * j); | |
let reverse_result = result.split("").reverse().join(""); | |
if(result == reverse_result){ | |
result = i * j; | |
if (final_result < i * j){ | |
final_result = i * j; | |
} | |
} | |
} | |
} | |
console.log(final_result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Title : Smallest Multiple | |
// Task : | |
// 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. | |
// What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20 ? | |
let result, number = 0, | |
process = true, | |
divisible = Array(20).fill(1).map( (v,i) => v+i); | |
while(process){ | |
number ++; | |
let is_continue = false; | |
divisible.forEach( v => { | |
if(number % v !== 0) is_continue = true; | |
}); | |
if(is_continue){ | |
continue; | |
} | |
result = number; | |
process = false; | |
} | |
console.log(result); | |
// 232792560 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Title: Sum square difference | |
// Task: | |
// <p>The sum of the squares of the first ten natural numbers is,</p> | |
// $$1^2 + 2^2 + ... + 10^2 = 385$$ | |
// <p>The square of the sum of the first ten natural numbers is,</p> | |
// $$(1 + 2 + ... + 10)^2 = 55^2 = 3025$$ | |
// <p>Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025 - 385 = 2640$.</p> | |
// <p>Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.</p> | |
const sum = (num, q=1) => { | |
return (num == 0) ? 0 : (num ** q) + sum(num-1,q); | |
} | |
let first_number = sum(100) ** 2; | |
let second_number = sum(100,2); | |
console.log(first_number - second_number); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment