Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-20-factorial-digit-sum.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

1.8 KiB
Raw Blame History

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f3801000cf542c50fe93 5 Problem 20: Factorial digit sum 301839

Description

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 n!

Instructions

Tests

tests:
  - text: <code>sumFactorialDigits(10)</code> should return a number.
    testString: assert(typeof sumFactorialDigits(10) === 'number');
  - text: <code>sumFactorialDigits(10)</code> should return 27.
    testString: assert.strictEqual(sumFactorialDigits(10), 27);
  - text: <code>sumFactorialDigits(25)</code> should return 72.
    testString: assert.strictEqual(sumFactorialDigits(25), 72);
  - text: <code>sumFactorialDigits(50)</code> should return 216.
    testString: assert.strictEqual(sumFactorialDigits(50), 216);
  - text: <code>sumFactorialDigits(75)</code> should return 432.
    testString: assert.strictEqual(sumFactorialDigits(75), 432);
  - text: <code>sumFactorialDigits(100)</code> should return 648.
    testString: assert.strictEqual(sumFactorialDigits(100), 648);

Challenge Seed

function sumFactorialDigits(n) {
  // Good luck!
  return n;
}

sumFactorialDigits(100);

Solution

let factorial = (n) => n <= 1 ? BigInt(n) : BigInt(n) * BigInt(factorial(--n));

let sumDigits = n => n.toString().split('').map(x => parseInt(x)).reduce((a,b) => a + b);

function sumFactorialDigits(n) {
  return sumDigits(factorial(n));
}