From a9e2661dcdd29880d822a36071f9905d5a8184f8 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Tue, 5 Oct 2021 18:06:50 +0200 Subject: [PATCH 1/4] Project Euler : add solution for problem 16 (Power digit sum). --- Project-Euler/Problem016.js | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Project-Euler/Problem016.js diff --git a/Project-Euler/Problem016.js b/Project-Euler/Problem016.js new file mode 100644 index 0000000000..6adf3d9e63 --- /dev/null +++ b/Project-Euler/Problem016.js @@ -0,0 +1,45 @@ +/** + * Problem 16 - Power digit sum + * + * @see {@link https://projecteuler.net/problem=16} + * + * 2¹⁵ = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. + * + * What is the sum of the digits of the number 2¹⁰⁰⁰ ? + */ + +/** + * Returns the power digit sum of n^pow. + * + * @param {number} [n=2] + * @param {number} [pow=1000] + * @returns {number} + */ +const powerDigitSum = function (n = 2, pow = 1000) { + // The idea is to consider each digit (d*10^exp) separately, right-to-left. + // digits = [units, tens, ...] + + const digits = [n] + let p = 1 + + while (++p <= pow) { + let carry = 0 + for (let exp = 0; exp < digits.length; exp++) { + const prod = digits[exp] * n + carry + carry = Math.floor(prod / 10) + digits[exp] = prod % 10 + } + while (carry > 0) { + digits.push(carry % 10) + carry = Math.floor(carry / 10) + } + } + + // (digits are reversed but we only want the sum so it doesn't matter) + + return digits.reduce((prev, current) => prev + current, 0) +} + +console.log('Power digit sum of 2^1000 :', powerDigitSum()) + +module.exports = powerDigitSum From 22e194ed392fdc1f6677b258cbadc40403a59718 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Tue, 5 Oct 2021 18:11:21 +0200 Subject: [PATCH 2/4] Add tests for ProjectEuler problem 16 (Jest). --- Project-Euler/test/Problem016.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Project-Euler/test/Problem016.test.js diff --git a/Project-Euler/test/Problem016.test.js b/Project-Euler/test/Problem016.test.js new file mode 100644 index 0000000000..0b707ef4c9 --- /dev/null +++ b/Project-Euler/test/Problem016.test.js @@ -0,0 +1,16 @@ +const powerDigitSum = require('../Problem016') + +describe('Check Problem 16 - Power digit sum', () => { + it('Power digit sum of 2^15', () => { + expect(powerDigitSum(2, 15)).toBe(26) + }) + + it('Power digit sum of 2^1000', () => { + expect(powerDigitSum()).toBe(1366) + expect(powerDigitSum(2, 1000)).toBe(1366) + }) + + it('Power digit sum of 3^5000', () => { + expect(powerDigitSum(3, 5000)).toBe(11097) + }) +}) From 8afef50cb7b6b06c83e93fc4ea8d38df5fe6325c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 5 Oct 2021 16:12:48 +0000 Subject: [PATCH 3/4] Auto-update DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index d63e09657f..34e83af682 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -190,6 +190,7 @@ * [Problem013](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem013.js) * [Problem014](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem014.js) * [Problem015](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem015.js) + * [Problem016](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem016.js) * [Problem020](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem020.js) * [Problem1](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem1.js) * [Problem10](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem10.js) From 52de0391845c8c83a66b9d42e25cff1616e94e53 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Fri, 8 Oct 2021 13:21:01 +0200 Subject: [PATCH 4/4] Use ESM syntax and remove console.log() --- Project-Euler/Problem016.js | 4 +--- Project-Euler/test/Problem016.test.js | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Project-Euler/Problem016.js b/Project-Euler/Problem016.js index 6adf3d9e63..d3c92f3eeb 100644 --- a/Project-Euler/Problem016.js +++ b/Project-Euler/Problem016.js @@ -40,6 +40,4 @@ const powerDigitSum = function (n = 2, pow = 1000) { return digits.reduce((prev, current) => prev + current, 0) } -console.log('Power digit sum of 2^1000 :', powerDigitSum()) - -module.exports = powerDigitSum +export { powerDigitSum } diff --git a/Project-Euler/test/Problem016.test.js b/Project-Euler/test/Problem016.test.js index 0b707ef4c9..9bb229f1a0 100644 --- a/Project-Euler/test/Problem016.test.js +++ b/Project-Euler/test/Problem016.test.js @@ -1,4 +1,4 @@ -const powerDigitSum = require('../Problem016') +import { powerDigitSum } from '../Problem016' describe('Check Problem 16 - Power digit sum', () => { it('Power digit sum of 2^15', () => {