Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 572a9b1

Browse files
chore: Merge pull request #747 from lvlte/ProjectEuler/020
Project Euler - Problem 20 (fix + test)
2 parents cbf28a7 + 2c273ba commit 572a9b1

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

‎Project-Euler/Problem020.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
/*
2-
Factorial digit sum
1+
/**
2+
* Problem 20 - Factorial digit sum
3+
*
4+
* @see {@link https://projecteuler.net/problem=20}
5+
*
6+
* n! means n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1
7+
*
8+
* For example, 10! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800,
9+
* and the sum of the digits in the number 10! is 3 +たす 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27
10+
*
11+
* Find the sum of the digits in the number 100!
12+
*/
313

4-
n! means n ×ばつ (n − 1) ×ばつ ... ×ばつ 3 ×ばつ 2 ×ばつ 1
14+
const factorialDigitSum = (n = 100) => {
15+
// Consider each digit*10^exp separately, right-to-left ([units, tens, ...]).
16+
const digits = [1]
517

6-
For example, 10! = 10 ×ばつ 9 ×ばつ ... ×ばつ 3 ×ばつかける 2 ×ばつかける 1 = 3628800,
7-
and the sum of the digits in the number 10! is 3 +たす 6 +たす 2 +たす 8 +たす 8 +たす 0 +たす 0 = 27.
18+
for (let x = 2; x <= n; x++) {
19+
let carry = 0
20+
for (let exp = 0; exp < digits.length; exp++) {
21+
const prod = digits[exp] * x + carry
22+
carry = Math.floor(prod / 10)
23+
digits[exp] = prod % 10
24+
}
25+
while (carry > 0) {
26+
digits.push(carry % 10)
27+
carry = Math.floor(carry / 10)
28+
}
29+
}
830

9-
Find the sum of the digits in the number 100!
10-
*/
31+
// (digits are reversed but we only want the sum so it doesn't matter)
1132

12-
const findFactorialDigitSum = (num) => {
13-
let result = 0
14-
const stringifiedNumber = factorize(num).toLocaleString('fullwide', { useGrouping: false })
15-
stringifiedNumber.split('').map(num => { result += Number(num) })
16-
return result
33+
return digits.reduce((prev, current) => prev + current, 0)
1734
}
1835

19-
const factorize = (num) => num === 0 ? 1 : num * factorize(num - 1)
20-
21-
console.log(findFactorialDigitSum(100))
36+
export { factorialDigitSum }

‎Project-Euler/test/Problem020.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { factorialDigitSum } from '../Problem020'
2+
3+
describe('Check Problem 20 - Factorial digit sum', () => {
4+
it('Factorial digit sum of 10!', () => {
5+
expect(factorialDigitSum(10)).toBe(27)
6+
})
7+
8+
it('Factorial digit sum of 100!', () => {
9+
expect(factorialDigitSum()).toBe(648)
10+
expect(factorialDigitSum(100)).toBe(648)
11+
})
12+
13+
it('Factorial digit sum of 1000!', () => {
14+
expect(factorialDigitSum(1000)).toBe(10539)
15+
})
16+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /