From 7121a6d96e0ca231438aa9cb74d58b86e2c129ac Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Tue, 5 Oct 2021 19:28:17 +0200 Subject: [PATCH 1/4] Project Euler : fix solution for problem 20 (Factorial digit sum). --- Project-Euler/Problem020.js | 46 ++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/Project-Euler/Problem020.js b/Project-Euler/Problem020.js index fc92df4f9c..fa67bec807 100644 --- a/Project-Euler/Problem020.js +++ b/Project-Euler/Problem020.js @@ -1,21 +1,39 @@ -/* -Factorial digit sum +/** + * Problem 20 - Factorial digit sum + * + * @see {@link https://projecteuler.net/problem=20} + * + * n! means n × (n − 1) × ... × 3 × 2 × 1 + * + * For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, + * and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27 + * + * Find the sum of the digits in the number 100! + */ -n! means n × (n − 1) × ... × 3 × 2 × 1 +const factorialDigitSum = function (n = 100) { -For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, -and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + // Consider each digit*10^exp separately, right-to-left ([units, tens, ...]). + let digits = [1]; -Find the sum of the digits in the number 100! -*/ + for (let x=2; x<=n; x++) { + let carry = 0; + for (let exp=0; exp 0) { + digits.push(carry%10); + carry = Math.floor(carry/10); + } + } -const findFactorialDigitSum = (num) => { - let result = 0 - const stringifiedNumber = factorize(num).toLocaleString('fullwide', { useGrouping: false }) - stringifiedNumber.split('').map(num => { result += Number(num) }) - return result + // (digits are reversed but we only want the sum so it doesn't matter) + + return digits.reduce((prev, current) => prev + current, 0) } -const factorize = (num) => num === 0 ? 1 : num * factorize(num - 1) +console.log('Factorial digit sum of 100! :', factorialDigitSum()) -console.log(findFactorialDigitSum(100)) +module.exports = factorialDigitSum From 70275154757f5c461b92f432527e8e6284d073b5 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Tue, 5 Oct 2021 19:29:07 +0200 Subject: [PATCH 2/4] Add tests for ProjectEuler problem 20 (Jest). --- Project-Euler/test/Problem020.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Project-Euler/test/Problem020.test.js diff --git a/Project-Euler/test/Problem020.test.js b/Project-Euler/test/Problem020.test.js new file mode 100644 index 0000000000..5eaa083dca --- /dev/null +++ b/Project-Euler/test/Problem020.test.js @@ -0,0 +1,16 @@ +const factorialDigitSum = require('../Problem020') + +describe('Check Problem 20 - Factorial digit sum', () => { + it('Factorial digit sum of 10!', () => { + expect(factorialDigitSum(10)).toBe(27) + }) + + it('Factorial digit sum of 100!', () => { + expect(factorialDigitSum()).toBe(648) + expect(factorialDigitSum(100)).toBe(648) + }) + + it('Factorial digit sum of 1000!', () => { + expect(factorialDigitSum(1000)).toBe(10539) + }) +}) From 697c840cfa28cfad4102a2272d7ad1583bd229c8 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Tue, 5 Oct 2021 19:50:30 +0200 Subject: [PATCH 3/4] Fix syntax (using JavaScript Standard Style) --- Project-Euler/Problem020.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Project-Euler/Problem020.js b/Project-Euler/Problem020.js index fa67bec807..3b31502f86 100644 --- a/Project-Euler/Problem020.js +++ b/Project-Euler/Problem020.js @@ -11,21 +11,20 @@ * Find the sum of the digits in the number 100! */ -const factorialDigitSum = function (n = 100) { - +const factorialDigitSum = (n = 100) => { // Consider each digit*10^exp separately, right-to-left ([units, tens, ...]). - let digits = [1]; + const digits = [1] - for (let x=2; x<=n; x++) { - let carry = 0; - for (let exp=0; exp 0) { - digits.push(carry%10); - carry = Math.floor(carry/10); + digits.push(carry % 10) + carry = Math.floor(carry / 10) } } From 2c273ba6621c34382d1e1c050a80d360d04e44b4 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:26:10 +0200 Subject: [PATCH 4/4] Use ESM syntax and remove console.log() --- Project-Euler/Problem020.js | 4 +--- Project-Euler/test/Problem020.test.js | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Project-Euler/Problem020.js b/Project-Euler/Problem020.js index 3b31502f86..256a4fb5de 100644 --- a/Project-Euler/Problem020.js +++ b/Project-Euler/Problem020.js @@ -33,6 +33,4 @@ const factorialDigitSum = (n = 100) => { return digits.reduce((prev, current) => prev + current, 0) } -console.log('Factorial digit sum of 100! :', factorialDigitSum()) - -module.exports = factorialDigitSum +export { factorialDigitSum } diff --git a/Project-Euler/test/Problem020.test.js b/Project-Euler/test/Problem020.test.js index 5eaa083dca..9a4c31e186 100644 --- a/Project-Euler/test/Problem020.test.js +++ b/Project-Euler/test/Problem020.test.js @@ -1,4 +1,4 @@ -const factorialDigitSum = require('../Problem020') +import { factorialDigitSum } from '../Problem020' describe('Check Problem 20 - Factorial digit sum', () => { it('Factorial digit sum of 10!', () => {