-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Project Euler - Problem 20 (fix + test) #747
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7121a6d
Project Euler : fix solution for problem 20 (Factorial digit sum).
lvlte 7027515
Add tests for ProjectEuler problem 20 (Jest).
lvlte 697c840
Fix syntax (using JavaScript Standard Style)
lvlte 2c273ba
Use ESM syntax and remove console.log()
lvlte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,38 @@ | ||
/* | ||
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 = (n = 100) => { | ||
// Consider each digit*10^exp separately, right-to-left ([units, tens, ...]). | ||
const digits = [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. | ||
for (let x = 2; x <= n; x++) { | ||
let carry = 0 | ||
for (let exp = 0; exp < digits.length; exp++) { | ||
const prod = digits[exp] * x + carry | ||
carry = Math.floor(prod / 10) | ||
digits[exp] = prod % 10 | ||
} | ||
while (carry > 0) { | ||
digits.push(carry % 10) | ||
carry = Math.floor(carry / 10) | ||
} | ||
} | ||
|
||
Find the sum of the digits in the number 100! | ||
*/ | ||
// (digits are reversed but we only want the sum so it doesn't matter) | ||
|
||
const findFactorialDigitSum = (num) => { | ||
let result = 0 | ||
const stringifiedNumber = factorize(num).toLocaleString('fullwide', { useGrouping: false }) | ||
stringifiedNumber.split('').map(num => { result += Number(num) }) | ||
return result | ||
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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const factorialDigitSum = require('../Problem020') | ||
raklaptudirm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.