From 563866d55515938a57a3eb7575a06076613c2b70 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:35 +0100 Subject: [PATCH] (euler) make puzzles functional --- euler/package.json | 1 + euler/run.js | 10 ++++++- euler/src/1 - Multiples of 3 or 5.ts | 17 +++++------ euler/src/2 - Even Fibonacci Numbers.ts | 27 +++++++++++------- euler/src/3 - Largest Prime Factor.ts | 17 ++++++----- euler/src/4 - Largest Palindrome Number.ts | 33 +++++++++++----------- euler/yarn.lock | 12 +++++++- 7 files changed, 73 insertions(+), 44 deletions(-) diff --git a/euler/package.json b/euler/package.json index 82085ac..d150aa3 100644 --- a/euler/package.json +++ b/euler/package.json @@ -8,6 +8,7 @@ "devDependencies": { "@types/node": "^16.11.6", "chalk": "^4.1.2", + "execution-time": "^1.4.1", "gulp": "^4.0.2", "gulp-cli": "^2.3.0", "gulp-rename": "^2.0.0", diff --git a/euler/run.js b/euler/run.js index 6745ef1..a18e860 100644 --- a/euler/run.js +++ b/euler/run.js @@ -1,6 +1,7 @@ const fs = require('fs'); const chalk = require('chalk'); const { spawnSync } = require('child_process'); +const perf = require('execution-time')(); const run = (file, puzzleName) => { // Calculate the length of the divider for the puzzle @@ -10,12 +11,19 @@ const run = (file, puzzleName) => { divider += '-'; } - // Log output + // Styling console.log(divider); console.log(chalk.bold(chalk.greenBright(puzzleName))); console.log(divider); + // Execute the file + perf.start(); spawnSync('node', [`build/${file}`], { shell: true, stdio: 'inherit' }); + const results = perf.stop(); + + // Print time results + console.log(); + console.log(chalk.bold(chalk.yellow(`Executed in ${results.words}`))); }; // Get files diff --git a/euler/src/1 - Multiples of 3 or 5.ts b/euler/src/1 - Multiples of 3 or 5.ts index cb9d16a..e656063 100644 --- a/euler/src/1 - Multiples of 3 or 5.ts +++ b/euler/src/1 - Multiples of 3 or 5.ts @@ -1,12 +1,13 @@ import { calcSum } from './utils'; -const below = 1000; -const multiplesOf = [3, 5]; -const toAdd: Set = new Set(); +const multiplesOf = (numbers: number[], upperBound: number) => { + const results: Set = new Set(); -// Find all of the multiples of 3 and 5 below 1000 -for (let i = 1; i < below; i++) { - multiplesOf.forEach(num => (i % num == 0 ? toAdd.add(i) : null)); -} + for (let i = 1; i < upperBound; i++) { + numbers.forEach(num => (i % num == 0 ? results.add(i) : null)); + } -console.log(calcSum(Array.from(toAdd))); + return Array.from(results); +}; + +console.log(calcSum(multiplesOf([3, 5], 1000))); diff --git a/euler/src/2 - Even Fibonacci Numbers.ts b/euler/src/2 - Even Fibonacci Numbers.ts index 5ac8d17..ea77a06 100644 --- a/euler/src/2 - Even Fibonacci Numbers.ts +++ b/euler/src/2 - Even Fibonacci Numbers.ts @@ -1,15 +1,22 @@ import { calcSum } from './utils'; -const sequence = [1, 2]; -const max = 4000000; +const fibonacciNumbers = (upperBound: number) => { + const sequence = [1, 2]; -// Keep making new nujm -while (sequence[sequence.length - 1] < max) { - const newValue = sequence[sequence.length - 1] + sequence[sequence.length - 2]; + // Keep making new numbers in the sequence until we hit the upper bound + while (sequence[sequence.length - 1] < upperBound) { + const newValue = sequence[sequence.length - 1] + sequence[sequence.length - 2]; + sequence.push(newValue); + } - sequence.push(newValue); -} + return sequence; +}; -// Filter out the even numbers and find the sum -const even = sequence.filter(n => n % 2 === 0); -console.log(calcSum(even)); +const evenFibonacciNumbers = (upperBound: number) => { + const sequence = fibonacciNumbers(upperBound); + const even = sequence.filter(n => n % 2 === 0); + + return even; +}; + +console.log(calcSum(evenFibonacciNumbers(4000000))); diff --git a/euler/src/3 - Largest Prime Factor.ts b/euler/src/3 - Largest Prime Factor.ts index 0d40952..a8c3808 100644 --- a/euler/src/3 - Largest Prime Factor.ts +++ b/euler/src/3 - Largest Prime Factor.ts @@ -1,9 +1,12 @@ -let number = 600851475143; -let i = 2; +const largestPrimeFactor = (number: number) => { + let i = 2; -while (i * i <= number) { - if (number % i) i += 1; - else number = Math.floor(number / i); -} + while (i * i <= number) { + if (number % i) i += 1; + else number = Math.floor(number / i); + } -console.log(number); + return number; +}; + +console.log(largestPrimeFactor(600851475143)); diff --git a/euler/src/4 - Largest Palindrome Number.ts b/euler/src/4 - Largest Palindrome Number.ts index 0bbc285..20051c4 100644 --- a/euler/src/4 - Largest Palindrome Number.ts +++ b/euler/src/4 - Largest Palindrome Number.ts @@ -1,22 +1,21 @@ -const biggest = 999; -const smallest = 100; +const largestPallidromeNumber = (lowerBound: number, upperBound: number) => { + // Work out all of the products of 3 digit numbers + const products: number[] = []; -// Work out all of the products of 3 digit numbers -const products: number[] = []; - -for (let i = smallest; i < biggest + 1; i++) { - for (let j = smallest; j < biggest + 1; j++) { - products.push(i * j); + for (let i = lowerBound; i < upperBound + 1; i++) { + for (let j = lowerBound; j < upperBound + 1; j++) { + products.push(i * j); + } } -} -// Filter for palindromic numbers -const palindromic = products.filter( - x => x.toString() === x.toString().split('').reverse().join('') -); + // Filter for palindromic numbers + const palindromic = products.filter( + x => x.toString() === x.toString().split('').reverse().join('') + ); -// Find the biggest palindrome number -const sorted = palindromic.sort((a, b) => b - a); -const largest = sorted[0]; + // Find the biggest palindrome number + const sorted = palindromic.sort((a, b) => b - a); + return sorted[0]; +}; -console.log(largest); +console.log(largestPallidromeNumber(100, 999)); diff --git a/euler/yarn.lock b/euler/yarn.lock index fa97630..55b6e75 100644 --- a/euler/yarn.lock +++ b/euler/yarn.lock @@ -926,6 +926,15 @@ __metadata: languageName: node linkType: hard +"execution-time@npm:^1.4.1": + version: 1.4.1 + resolution: "execution-time@npm:1.4.1" + dependencies: + pretty-hrtime: ^1.0.3 + checksum: 0a5ef5604fc1ed9f626126443dfb52cd94926a657384433a3737e7878e1697e9b37e04e63a22e2ae2f6ac6bd20261f58fb8c890388f6d3fdf906425fca98be3d + languageName: node + linkType: hard + "expand-brackets@npm:^2.1.4": version: 2.1.4 resolution: "expand-brackets@npm:2.1.4" @@ -2627,7 +2636,7 @@ __metadata: languageName: node linkType: hard -"pretty-hrtime@npm:^1.0.0": +"pretty-hrtime@npm:^1.0.0, pretty-hrtime@npm:^1.0.3": version: 1.0.3 resolution: "pretty-hrtime@npm:1.0.3" checksum: bae0e6832fe13c3de43d1a3d43df52bf6090499d74dc65a17f5552cb1a94f1f8019a23284ddf988c3c408a09678d743901e1d8f5b7a71bec31eeeac445bef371 @@ -2647,6 +2656,7 @@ __metadata: dependencies: "@types/node": ^16.11.6 chalk: ^4.1.2 + execution-time: ^1.4.1 gulp: ^4.0.2 gulp-cli: ^2.3.0 gulp-rename: ^2.0.0