From 039be699ec48db9460270735ee6297ed08188f1a Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:35 +0100 Subject: [PATCH] (euler) - #10 and export {} --- euler/gulpfile.js | 2 +- euler/src/1 - Multiples of 3 or 5.ts | 4 ++- euler/src/10 - Sumation of Primes.ts | 35 ++++++++++++++++++++ euler/src/2 - Even Fibonacci Numbers.ts | 4 ++- euler/src/3 - Largest Prime Factor.ts | 2 ++ euler/src/4 - Largest Palindrome Number.ts | 2 ++ euler/src/5 - Smallest Number.ts | 2 ++ euler/src/6 - Sum Square Difference.ts | 4 ++- euler/src/7 - 10001st Prime.ts | 2 ++ euler/src/8 - Largest Product in a Series.ts | 2 ++ euler/src/9 - Special Pythagorean Triplet.ts | 2 ++ euler/src/utils.ts | 4 --- 12 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 euler/src/10 - Sumation of Primes.ts delete mode 100644 euler/src/utils.ts diff --git a/euler/gulpfile.js b/euler/gulpfile.js index cdb6904..0afb462 100644 --- a/euler/gulpfile.js +++ b/euler/gulpfile.js @@ -13,7 +13,7 @@ gulp.task('build', () => { .pipe( rename(path => { if (path.basename === 'utils') return; - path.basename = path.basename.substr(0, 1); + path.basename = path.basename.split('-')[0].trim(); }) ) .pipe(gulp.dest('build')); diff --git a/euler/src/1 - Multiples of 3 or 5.ts b/euler/src/1 - Multiples of 3 or 5.ts index e656063..face2b0 100644 --- a/euler/src/1 - Multiples of 3 or 5.ts +++ b/euler/src/1 - Multiples of 3 or 5.ts @@ -1,4 +1,6 @@ -import { calcSum } from './utils'; +export {}; + +const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b); const multiplesOf = (numbers: number[], upperBound: number) => { const results: Set = new Set(); diff --git a/euler/src/10 - Sumation of Primes.ts b/euler/src/10 - Sumation of Primes.ts new file mode 100644 index 0000000..39f88b2 --- /dev/null +++ b/euler/src/10 - Sumation of Primes.ts @@ -0,0 +1,35 @@ +export {}; + +const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b); + +// Sieve of Eratosthenes time!!! +const sumOfPrimes = (upperBound: number) => { + let array: boolean[] = []; + let upperLimit = Math.sqrt(upperBound); + let output: number[] = []; + + // Make an array from 2 to (n - 1) of truthy values + for (var i = 0; i < upperBound; i++) { + array.push(true); + } + + // Remove multiples of primes starting from 2, 3, 5,... + for (var i = 2; i <= upperLimit; i++) { + if (array[i]) { + for (var j = i * i; j < upperBound; j += i) { + array[j] = false; + } + } + } + + // All array[i] set to true are primes + for (var i = 2; i < upperBound; i++) { + if (array[i]) { + output.push(i); + } + } + + return calcSum(output); +}; + +console.log(sumOfPrimes(2000000)); diff --git a/euler/src/2 - Even Fibonacci Numbers.ts b/euler/src/2 - Even Fibonacci Numbers.ts index ea77a06..5a72fb4 100644 --- a/euler/src/2 - Even Fibonacci Numbers.ts +++ b/euler/src/2 - Even Fibonacci Numbers.ts @@ -1,4 +1,6 @@ -import { calcSum } from './utils'; +export {}; + +const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b); const fibonacciNumbers = (upperBound: number) => { const sequence = [1, 2]; diff --git a/euler/src/3 - Largest Prime Factor.ts b/euler/src/3 - Largest Prime Factor.ts index a8c3808..30f085d 100644 --- a/euler/src/3 - Largest Prime Factor.ts +++ b/euler/src/3 - Largest Prime Factor.ts @@ -1,3 +1,5 @@ +export {}; + const largestPrimeFactor = (number: number) => { let i = 2; diff --git a/euler/src/4 - Largest Palindrome Number.ts b/euler/src/4 - Largest Palindrome Number.ts index 20051c4..3169614 100644 --- a/euler/src/4 - Largest Palindrome Number.ts +++ b/euler/src/4 - Largest Palindrome Number.ts @@ -1,3 +1,5 @@ +export {}; + const largestPallidromeNumber = (lowerBound: number, upperBound: number) => { // Work out all of the products of 3 digit numbers const products: number[] = []; diff --git a/euler/src/5 - Smallest Number.ts b/euler/src/5 - Smallest Number.ts index df4f552..6f68799 100644 --- a/euler/src/5 - Smallest Number.ts +++ b/euler/src/5 - Smallest Number.ts @@ -1,3 +1,5 @@ +export {}; + /** * Is x disible to n? */ diff --git a/euler/src/6 - Sum Square Difference.ts b/euler/src/6 - Sum Square Difference.ts index 27da7f4..ab3da59 100644 --- a/euler/src/6 - Sum Square Difference.ts +++ b/euler/src/6 - Sum Square Difference.ts @@ -1,4 +1,6 @@ -import { calcSum } from './utils'; +export {}; + +const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b); const sumOfSquares = (lowerBound: number, upperBound: number) => { // Calculate the square number of all the numbers between the bounds diff --git a/euler/src/7 - 10001st Prime.ts b/euler/src/7 - 10001st Prime.ts index 78b3b1c..af09fc6 100644 --- a/euler/src/7 - 10001st Prime.ts +++ b/euler/src/7 - 10001st Prime.ts @@ -1,3 +1,5 @@ +export {}; + const isPrime = (number: number) => { for (var i = 2; i < number; i++) { if (number % i === 0) return false; diff --git a/euler/src/8 - Largest Product in a Series.ts b/euler/src/8 - Largest Product in a Series.ts index bf89397..35ec683 100644 --- a/euler/src/8 - Largest Product in a Series.ts +++ b/euler/src/8 - Largest Product in a Series.ts @@ -1,3 +1,5 @@ +export {}; + const largestProduct = (number: bigint | number, adjacentDigits: number) => { let cursorIndex = 0; const selections: number[][] = []; diff --git a/euler/src/9 - Special Pythagorean Triplet.ts b/euler/src/9 - Special Pythagorean Triplet.ts index 411c251..c6e3983 100644 --- a/euler/src/9 - Special Pythagorean Triplet.ts +++ b/euler/src/9 - Special Pythagorean Triplet.ts @@ -1,3 +1,5 @@ +export {}; + // a + b + c = 1000 // we need a * b * c // pythagorean triplet: a < b < c diff --git a/euler/src/utils.ts b/euler/src/utils.ts deleted file mode 100644 index 0527b6c..0000000 --- a/euler/src/utils.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Calculates the sum of an array of numbers. - */ -export const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);