Remove live code & console.log, leave examples as comments (ProjectEuler, Recursive).

This commit is contained in:
Eric Lavault
2021-10-10 18:18:25 +02:00
parent 9212e6d684
commit 5f45b540b9
17 changed files with 48 additions and 98 deletions

View File

@ -107,13 +107,11 @@ const numbers = [
53503534226472524250874054075591789781264330331690
]
const findFirstTenDigitsOfSum = () => {
const sum = numbers.reduce((prev, current) => {
export const findFirstTenDigitsOfSum = (N = numbers) => {
const sum = N.reduce((prev, current) => {
current += prev
return current
}, 0)
return sum.toLocaleString('fullwide', { useGrouping: false }).split('').slice(0, 10).join('')
}
console.log(findFirstTenDigitsOfSum())

View File

@ -31,10 +31,10 @@ const getCollatzSequenceLength = (num, seqLength) => {
}
}
const findLongestCollatzSequence = () => {
export const findLongestCollatzSequence = (limit = 1_000_000) => {
let startingPointForLargestSequence = 1
let largestSequenceLength = 1
for (let i = 2; i < 1000000; i++) {
for (let i = 2; i < limit; i++) {
const currentSequenceLength = getCollatzSequenceLength(i, 1)
if (currentSequenceLength > largestSequenceLength) {
startingPointForLargestSequence = i
@ -43,5 +43,3 @@ const findLongestCollatzSequence = () => {
}
return startingPointForLargestSequence
}
console.log(findLongestCollatzSequence())

View File

@ -6,7 +6,7 @@ How many such routes are there through a 20×20 grid?
// A lattice path is composed of horizontal and vertical lines that pass through lattice points.
const latticePath = (gridSize) => {
export const latticePath = (gridSize) => {
let paths
for (let i = 1, paths = 1; i <= gridSize; i++) {
paths = paths * (gridSize + i) / i
@ -14,4 +14,6 @@ const latticePath = (gridSize) => {
// The total number of paths can be found using the binomial coefficient (b+a)/a.
return paths
}
console.log(latticePath(20)) // output = 137846528820
// > latticePath(20))
// 137846528820

View File

@ -4,10 +4,9 @@ const PHI = (1 + SQ5) / 2 // definition of PHI
// theoretically it should take O(1) constant amount of time as long
// arithmetic calculations are considered to be in constant amount of time
const EvenFibonacci = (limit) => {
export const EvenFibonacci = (limit) => {
const highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI))
const n = Math.floor(highestIndex / 3)
return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -
((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5
}
console.log(EvenFibonacci(4e6)) // Sum of even Fibonacci upto 4 Million

View File

@ -2,7 +2,7 @@
/* 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.
*/
const largestPalindromic = (digits) => {
export const largestPalindromic = (digits) => {
let i
let n
let m
@ -43,4 +43,4 @@ const largestPalindromic = (digits) => {
return NaN // returning not a number, if any such case arise
}
console.log(largestPalindromic(3))

View File

@ -5,7 +5,7 @@ Smallest multiple
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
*/
const findSmallestMultiple = () => {
export const findSmallestMultiple = () => {
const divisors = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
let num = 21
let result
@ -18,5 +18,3 @@ const findSmallestMultiple = () => {
return result
}
console.log(findSmallestMultiple())

View File

@ -1,8 +1,6 @@
// https://projecteuler.net/problem=6
const num = 100 // number we are checking; change to 10 to check 10 from example
const squareDifference = (num) => {
export const squareDifference = (num = 100) => {
let sumOfSquares = 0
let sums = 0
for (let i = 1; i <= num; i++) {
@ -11,5 +9,3 @@ const squareDifference = (num) => {
}
return (sums ** 2) - sumOfSquares // difference of square of the total sum and sum of squares
}
console.log(squareDifference(num))

View File

@ -1,10 +1,7 @@
// https://projecteuler.net/problem=7
// My approach does not use the Sieve of Eratosthenes but that is another common way to approach this problem. Sieve of Atkin is another possibility as well.
const num = 10001
const primes = [2, 3, 5, 7, 11, 13] // given list of primes you start with
const calculatePrime = (num) => {
export const calculatePrime = (num = 10001, primes = [2, 3, 5, 7, 11, 13]) => {
// Calculate each next prime by checking each number to see what it's divisible by
let count = primes.length // count number of primes calculated
let current = primes[count - 1] + 1 // current number being assessed if prime
@ -27,5 +24,3 @@ const calculatePrime = (num) => {
}
return primes[num - 1]
}
console.log(calculatePrime(num))

View File

@ -12,7 +12,7 @@ Find the product abc.
const isPythagoreanTriplet = (a, b, c) => Math.pow(a, 2) + Math.pow(b, 2) === Math.pow(c, 2)
const findSpecialPythagoreanTriplet = () => {
export const findSpecialPythagoreanTriplet = () => {
for (let a = 0; a < 1000; a++) {
for (let b = a + 1; b < 1000; b++) {
for (let c = b + 1; c < 1000; c++) {
@ -23,5 +23,3 @@ const findSpecialPythagoreanTriplet = () => {
}
}
}
console.log(findSpecialPythagoreanTriplet())