Skip to content

Commit

Permalink
Remove live code & console.log, leave examples as comments (ProjectEu…
Browse files Browse the repository at this point in the history
…ler, Recursive).
  • Loading branch information
lvlte committed Oct 10, 2021
1 parent 9212e6d commit 5f45b54
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 98 deletions.
6 changes: 2 additions & 4 deletions Project-Euler/Problem013.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
6 changes: 2 additions & 4 deletions Project-Euler/Problem014.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,5 +43,3 @@ const findLongestCollatzSequence = () => {
}
return startingPointForLargestSequence
}

console.log(findLongestCollatzSequence())
6 changes: 4 additions & 2 deletions Project-Euler/Problem015.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ 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
}
// 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
3 changes: 1 addition & 2 deletions Project-Euler/Problem2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions Project-Euler/Problem4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,4 +43,4 @@ const largestPalindromic = (digits) => {
return NaN // returning not a number, if any such case arise
}

console.log(largestPalindromic(3))

4 changes: 1 addition & 3 deletions Project-Euler/Problem5.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,5 +18,3 @@ const findSmallestMultiple = () => {

return result
}

console.log(findSmallestMultiple())
6 changes: 1 addition & 5 deletions Project-Euler/Problem6.js
Original file line number Diff line number Diff line change
@@ -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++) {
Expand All @@ -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))
7 changes: 1 addition & 6 deletions Project-Euler/Problem7.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,5 +24,3 @@ const calculatePrime = (num) => {
}
return primes[num - 1]
}

console.log(calculatePrime(num))
4 changes: 1 addition & 3 deletions Project-Euler/Problem9.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -23,5 +23,3 @@ const findSpecialPythagoreanTriplet = () => {
}
}
}

console.log(findSpecialPythagoreanTriplet())
7 changes: 1 addition & 6 deletions Recursive/BinaryEquivalent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
12 changes: 1 addition & 11 deletions Recursive/BinarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))
})()
9 changes: 1 addition & 8 deletions Recursive/EucledianGCD.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
20 changes: 11 additions & 9 deletions Recursive/FibonacciNumberRecursive.js
Original file line number Diff line number Diff line change
@@ -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))
})()
13 changes: 2 additions & 11 deletions Recursive/Palindrome.js
Original file line number Diff line number Diff line change
@@ -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()
}
Expand All @@ -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'))
})()
}
10 changes: 4 additions & 6 deletions Recursive/SubsequenceRecursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 12 additions & 10 deletions Recursive/TowerOfHanoi.js
Original file line number Diff line number Diff line change
@@ -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')
7 changes: 1 addition & 6 deletions Recursive/factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 5f45b54

Please sign in to comment.