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 53503534226472524250874054075591789781264330331690
] ]
const findFirstTenDigitsOfSum = () => { export const findFirstTenDigitsOfSum = (N = numbers) => {
const sum = numbers.reduce((prev, current) => { const sum = N.reduce((prev, current) => {
current += prev current += prev
return current return current
}, 0) }, 0)
return sum.toLocaleString('fullwide', { useGrouping: false }).split('').slice(0, 10).join('') 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 startingPointForLargestSequence = 1
let largestSequenceLength = 1 let largestSequenceLength = 1
for (let i = 2; i < 1000000; i++) { for (let i = 2; i < limit; i++) {
const currentSequenceLength = getCollatzSequenceLength(i, 1) const currentSequenceLength = getCollatzSequenceLength(i, 1)
if (currentSequenceLength > largestSequenceLength) { if (currentSequenceLength > largestSequenceLength) {
startingPointForLargestSequence = i startingPointForLargestSequence = i
@ -43,5 +43,3 @@ const findLongestCollatzSequence = () => {
} }
return startingPointForLargestSequence 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. // A lattice path is composed of horizontal and vertical lines that pass through lattice points.
const latticePath = (gridSize) => { export const latticePath = (gridSize) => {
let paths let paths
for (let i = 1, paths = 1; i <= gridSize; i++) { for (let i = 1, paths = 1; i <= gridSize; i++) {
paths = paths * (gridSize + i) / 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. // The total number of paths can be found using the binomial coefficient (b+a)/a.
return paths 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 // theoretically it should take O(1) constant amount of time as long
// arithmetic calculations are considered to be in constant amount of time // 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 highestIndex = Math.floor(Math.log(limit * SQ5) / Math.log(PHI))
const n = Math.floor(highestIndex / 3) const n = Math.floor(highestIndex / 3)
return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) - return ((PHI ** (3 * n + 3) - 1) / (PHI ** 3 - 1) -
((1 - PHI) ** (3 * n + 3) - 1) / ((1 - PHI) ** 3 - 1)) / SQ5 ((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. /* 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. Find the largest palindrome made from the product of two 3-digit numbers.
*/ */
const largestPalindromic = (digits) => { export const largestPalindromic = (digits) => {
let i let i
let n let n
let m let m
@ -43,4 +43,4 @@ const largestPalindromic = (digits) => {
return NaN // returning not a number, if any such case arise 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? 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] 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 num = 21
let result let result
@ -18,5 +18,3 @@ const findSmallestMultiple = () => {
return result return result
} }
console.log(findSmallestMultiple())

View File

@ -1,8 +1,6 @@
// https://projecteuler.net/problem=6 // https://projecteuler.net/problem=6
const num = 100 // number we are checking; change to 10 to check 10 from example export const squareDifference = (num = 100) => {
const squareDifference = (num) => {
let sumOfSquares = 0 let sumOfSquares = 0
let sums = 0 let sums = 0
for (let i = 1; i <= num; i++) { 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 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 // 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. // 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 export const calculatePrime = (num = 10001, primes = [2, 3, 5, 7, 11, 13]) => {
const primes = [2, 3, 5, 7, 11, 13] // given list of primes you start with
const calculatePrime = (num) => {
// Calculate each next prime by checking each number to see what it's divisible by // 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 count = primes.length // count number of primes calculated
let current = primes[count - 1] + 1 // current number being assessed if prime let current = primes[count - 1] + 1 // current number being assessed if prime
@ -27,5 +24,3 @@ const calculatePrime = (num) => {
} }
return primes[num - 1] 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 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 a = 0; a < 1000; a++) {
for (let b = a + 1; b < 1000; b++) { for (let b = a + 1; b < 1000; b++) {
for (let c = b + 1; c < 1000; c++) { for (let c = b + 1; c < 1000; c++) {
@ -23,5 +23,3 @@ const findSpecialPythagoreanTriplet = () => {
} }
} }
} }
console.log(findSpecialPythagoreanTriplet())

View File

@ -13,14 +13,9 @@
* *
*/ */
const binaryEquivalent = (num) => { export const binaryEquivalent = (num) => {
if (num === 0 || num === 1) { if (num === 0 || num === 1) {
return String(num) return String(num)
} }
return binaryEquivalent(Math.floor(num / 2)) + String(num % 2) return binaryEquivalent(Math.floor(num / 2)) + String(num % 2)
} }
// Driver Code
const num = 6
const ans = binaryEquivalent(num)
console.log(ans)

View File

@ -2,7 +2,7 @@
// https://en.wikipedia.org/wiki/Binary_search_algorithm // https://en.wikipedia.org/wiki/Binary_search_algorithm
// Search the integer inside the sorted integers array using Binary Search Algorithm // Search the integer inside the sorted integers array using Binary Search Algorithm
const BinarySearch = (intArr, searchQuery) => { export const BinarySearch = (intArr, searchQuery) => {
if (searchQuery === null || searchQuery === undefined || intArr.length === 0) { if (searchQuery === null || searchQuery === undefined || intArr.length === 0) {
return false return false
} }
@ -17,13 +17,3 @@ const BinarySearch = (intArr, searchQuery) => {
return false return false
} }
} }
// testing
(() => {
console.log('Number Present with odd array length: 5 = ', BinarySearch([1, 2, 3, 4, 5, 6, 7], 5))
console.log('Number Present with even array length: 5 = ', BinarySearch([1, 2, 4, 5, 6], 5))
console.log('Number Present with only single element: 5 = ', BinarySearch([5], 5))
console.log('Number Not Present: 0 = ', BinarySearch([1, 2, 3, 4, 5], 0))
console.log('Undefined number search query = ', BinarySearch([1, 2, 3, 4, 5]))
console.log('With Empty array = ', BinarySearch([], 1))
})()

View File

@ -27,11 +27,4 @@ function euclideanGCDIterative (first, second) {
return first return first
} }
function main () { export { euclideanGCDIterative, euclideanGCDRecursive }
const first = 20
const second = 30
console.log('Recursive GCD for %d and %d is %d', first, second, euclideanGCDRecursive(first, second))
console.log('Iterative GCD for %d and %d is %d', first, second, euclideanGCDIterative(first, second))
}
main()

View File

@ -1,13 +1,15 @@
// https://en.wikipedia.org/wiki/Fibonacci_number // https://en.wikipedia.org/wiki/Fibonacci_number
const fibonacci = (N) => { /**
if (N === 0 || N === 1) return N * Return the N-th Fibonacci number
*
* @param {number} N
* @returns {number}
*/
export const fibonacci = (N) => {
if (N === 0 || N === 1) {
return N
}
return fibonacci(N - 2) + fibonacci(N - 1) return fibonacci(N - 2) + fibonacci(N - 1)
} }
// testing
(() => {
const number = 5
console.log(number + 'th Fibonacci number is ' + fibonacci(number))
})()

View File

@ -1,6 +1,6 @@
// Check whether the given string is Palindrome or not // Check whether the given string is Palindrome or not
const Palindrome = (str) => { export const Palindrome = (str) => {
if (typeof str !== 'string') { if (typeof str !== 'string') {
str = str.toString() str = str.toString()
} }
@ -18,13 +18,4 @@ const Palindrome = (str) => {
} else { } else {
return Palindrome(str.slice(1, str.length - 1)) return Palindrome(str.slice(1, str.length - 1))
} }
}; }
// testing
(() => {
console.log('Palindrome: String: a = ', Palindrome('a'))
console.log('Palindrome: String: abba = ', Palindrome('abba'))
console.log('Palindrome: String: ababa = ', Palindrome('ababa'))
console.log('Not Palindrome: String: abbxa = ', Palindrome('abbxa'))
console.log('Not Palindrome: String: abxa = ', Palindrome('abxa'))
})()

View File

@ -19,14 +19,12 @@
* https://en.wikipedia.org/wiki/Lexicographic_order * https://en.wikipedia.org/wiki/Lexicographic_order
*/ */
const subsequence = (str, seq, low) => { export const subsequence = (str, seq, low, output = []) => {
if (low <= str.length && str.length !== 0) { if (low <= str.length && str.length !== 0) {
console.log(seq) output.push(seq)
} }
for (let i = low; i < str.length; i++) { for (let i = low; i < str.length; i++) {
subsequence(str, seq + str[i], i + 1) subsequence(str, seq + str[i], i + 1, output)
} }
return output
} }
const str = 'abcd'
subsequence(str, '', 0)

View File

@ -1,16 +1,18 @@
// wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi // wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi
// Recursive Javascript function to solve tower of hanoi // Recursive Javascript function to solve tower of hanoi
function TowerOfHanoi (n, fromRod, toRod, auxRod) { export function TowerOfHanoi (n, from, to, aux, output = []) {
if (n === 1) { if (n === 1) {
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`) output.push(`Move disk 1 from rod ${from} to rod ${to}`)
return return output
} }
TowerOfHanoi(n - 1, fromRod, auxRod, toRod) TowerOfHanoi(n - 1, from, aux, to, output)
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`) output.push(`Move disk ${n} from rod ${from} to rod ${to}`)
TowerOfHanoi(n - 1, auxRod, toRod, fromRod) TowerOfHanoi(n - 1, aux, to, from, output)
return output
} }
// Driver code
const n = 4 // Driver code (A, C, B are the name of rods)
TowerOfHanoi(n, 'A', 'C', 'B')
// A, C, B are the name of rods // const n = 4
// TowerOfHanoi(n, 'A', 'C', 'B')

View File

@ -3,14 +3,9 @@
// 5! = 1*2*3*4*5 = 120 // 5! = 1*2*3*4*5 = 120
// 2! = 1*2 = 2 // 2! = 1*2 = 2
const factorial = (n) => { export const factorial = (n) => {
if (n === 0) { if (n === 0) {
return 1 return 1
} }
return n * factorial(n - 1) return n * factorial(n - 1)
} }
// testing
console.log(factorial(4))
console.log(factorial(15))
console.log(factorial(0))