Skip to content

Project Euler problem 020 solution #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Project-Euler/Problem020.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Factorial digit sum

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!
*/

const findFactorialDigitSum = (num) => {
let result = 0
const stringifiedNumber = factorize(num).toLocaleString('fullwide', { useGrouping: false })
stringifiedNumber.split('').map(num => { result += Number(num) })
return result
}

const factorize = (num) => num === 0 ? 1 : num * factorize(num - 1)

console.log(findFactorialDigitSum(100))