<div class="code">
<a href="https://projecteuler.net/problem=20">Project Euler</a> Solution
<br />Problem ID #20 : Factorial digit sum
<br />
<br />n! means n × (n − 1) × ... × 3 × 2 × 1
<br />For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
<br />and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
<br />
<br />Find the sum of the digits in the number 100!
</div>
<div id="main"></div>
var strResult = '<br />The factorial of 100 is: ';
var result = 0;
function factorial(number) {
if (number == 1) return 1;
return number * factorial(number-1);
}
function sumOfDigits(number) {
//takes the number and adds of the digits, as an example 321 would be 6 (3+2+1)
var str = number.toString();
var sum = 0;
for (var x = -1; ++x < str.length;) sum += +str[x];
return sum;
}
result = factorial(10);
strResult += result;
strResult += '<br/> And the sum of its digits is: ' + sumOfDigits(result);
$('#main').html(strResult);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.