diff --git a/Project-Euler/Problem013.js b/Project-Euler/Problem013.js index 26769ab9a3..01563186c4 100644 --- a/Project-Euler/Problem013.js +++ b/Project-Euler/Problem013.js @@ -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()) diff --git a/Project-Euler/Problem014.js b/Project-Euler/Problem014.js index 0331837d09..b0f5014029 100644 --- a/Project-Euler/Problem014.js +++ b/Project-Euler/Problem014.js @@ -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()) diff --git a/Project-Euler/Problem015.js b/Project-Euler/Problem015.js index d354047330..1861376244 100644 --- a/Project-Euler/Problem015.js +++ b/Project-Euler/Problem015.js @@ -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 diff --git a/Project-Euler/Problem2.js b/Project-Euler/Problem2.js index 35b011206b..5b8c3ddd51 100644 --- a/Project-Euler/Problem2.js +++ b/Project-Euler/Problem2.js @@ -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 diff --git a/Project-Euler/Problem4.js b/Project-Euler/Problem4.js index bcc4d880b6..d6ba7a722e 100644 --- a/Project-Euler/Problem4.js +++ b/Project-Euler/Problem4.js @@ -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)) + diff --git a/Project-Euler/Problem5.js b/Project-Euler/Problem5.js index 0ba30669a5..9b0020a754 100644 --- a/Project-Euler/Problem5.js +++ b/Project-Euler/Problem5.js @@ -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()) diff --git a/Project-Euler/Problem6.js b/Project-Euler/Problem6.js index 38dd31d950..673202acf7 100644 --- a/Project-Euler/Problem6.js +++ b/Project-Euler/Problem6.js @@ -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)) diff --git a/Project-Euler/Problem7.js b/Project-Euler/Problem7.js index debc75276b..2bb4eeef44 100644 --- a/Project-Euler/Problem7.js +++ b/Project-Euler/Problem7.js @@ -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)) diff --git a/Project-Euler/Problem9.js b/Project-Euler/Problem9.js index a877c89d86..60422f89e5 100644 --- a/Project-Euler/Problem9.js +++ b/Project-Euler/Problem9.js @@ -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()) diff --git a/Recursive/BinaryEquivalent.js b/Recursive/BinaryEquivalent.js index a3a8de0c9e..df92f7b3ff 100644 --- a/Recursive/BinaryEquivalent.js +++ b/Recursive/BinaryEquivalent.js @@ -13,14 +13,9 @@ * */ -const binaryEquivalent = (num) => { +export const binaryEquivalent = (num) => { if (num === 0 || num === 1) { return String(num) } return binaryEquivalent(Math.floor(num / 2)) + String(num % 2) } - -// Driver Code -const num = 6 -const ans = binaryEquivalent(num) -console.log(ans) diff --git a/Recursive/BinarySearch.js b/Recursive/BinarySearch.js index 1df6a2dec5..ef24dcba0d 100644 --- a/Recursive/BinarySearch.js +++ b/Recursive/BinarySearch.js @@ -2,7 +2,7 @@ // https://en.wikipedia.org/wiki/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) { return false } @@ -17,13 +17,3 @@ const BinarySearch = (intArr, searchQuery) => { 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)) -})() diff --git a/Recursive/EucledianGCD.js b/Recursive/EucledianGCD.js index 5f1c51a06d..9f4c4a79a0 100644 --- a/Recursive/EucledianGCD.js +++ b/Recursive/EucledianGCD.js @@ -27,11 +27,4 @@ function euclideanGCDIterative (first, second) { return first } -function main () { - 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() +export { euclideanGCDIterative, euclideanGCDRecursive } diff --git a/Recursive/FibonacciNumberRecursive.js b/Recursive/FibonacciNumberRecursive.js index e5c5bb95bb..f8d4637495 100644 --- a/Recursive/FibonacciNumberRecursive.js +++ b/Recursive/FibonacciNumberRecursive.js @@ -1,13 +1,15 @@ -// https://en.wikipedia.org/wiki/Fibonacci_number -const fibonacci = (N) => { - if (N === 0 || N === 1) return N +// https://en.wikipedia.org/wiki/Fibonacci_number +/** + * 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) } - -// testing -(() => { - const number = 5 - console.log(number + 'th Fibonacci number is ' + fibonacci(number)) -})() diff --git a/Recursive/Palindrome.js b/Recursive/Palindrome.js index 483fb012e2..9357135174 100644 --- a/Recursive/Palindrome.js +++ b/Recursive/Palindrome.js @@ -1,6 +1,6 @@ // Check whether the given string is Palindrome or not -const Palindrome = (str) => { +export const Palindrome = (str) => { if (typeof str !== 'string') { str = str.toString() } @@ -18,13 +18,4 @@ const Palindrome = (str) => { } else { 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')) -})() +} diff --git a/Recursive/SubsequenceRecursive.js b/Recursive/SubsequenceRecursive.js index e54798d1b8..c7bedcb7a6 100644 --- a/Recursive/SubsequenceRecursive.js +++ b/Recursive/SubsequenceRecursive.js @@ -19,14 +19,12 @@ * 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) { - console.log(seq) + output.push(seq) } 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) diff --git a/Recursive/TowerOfHanoi.js b/Recursive/TowerOfHanoi.js index 0dc3ec6072..e43c426d33 100644 --- a/Recursive/TowerOfHanoi.js +++ b/Recursive/TowerOfHanoi.js @@ -1,16 +1,18 @@ // wiki - https://en.wikipedia.org/wiki/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) { - console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`) - return + output.push(`Move disk 1 from rod ${from} to rod ${to}`) + return output } - TowerOfHanoi(n - 1, fromRod, auxRod, toRod) - console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`) - TowerOfHanoi(n - 1, auxRod, toRod, fromRod) + TowerOfHanoi(n - 1, from, aux, to, output) + output.push(`Move disk ${n} from rod ${from} to rod ${to}`) + TowerOfHanoi(n - 1, aux, to, from, output) + return output } -// Driver code -const n = 4 -TowerOfHanoi(n, 'A', 'C', 'B') -// A, C, B are the name of rods + +// Driver code (A, C, B are the name of rods) + +// const n = 4 +// TowerOfHanoi(n, 'A', 'C', 'B') diff --git a/Recursive/factorial.js b/Recursive/factorial.js index 0b1260c30d..ada7627c0d 100644 --- a/Recursive/factorial.js +++ b/Recursive/factorial.js @@ -3,14 +3,9 @@ // 5! = 1*2*3*4*5 = 120 // 2! = 1*2 = 2 -const factorial = (n) => { +export const factorial = (n) => { if (n === 0) { return 1 } return n * factorial(n - 1) } - -// testing -console.log(factorial(4)) -console.log(factorial(15)) -console.log(factorial(0))