# Factorial digit sum

## The challenge is to calculate the digit sum of the factorial of a number.

Example

Input: 10
Output: 27


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

You can expect the input to be an integer above 0. Output can be of any type, but the answer should be in the standard base of the coding language.

Test cases:

10    27
19    45
469   4140
985   10053


N.B. Some languages can not support large numbers above 32-bit integers; for those languages you will not be expected to calculate large factorials.

OEIS link here thanks to Martin Ender

This is , so shortest code in characters wins!

• What's the maximum input number to expect? With 32-bit integers in R this challenge can't be solved accurately past n>21 Nov 23 '16 at 15:22
• @Billywob For R then you will only need to go to 20. I shall edit question to reflect this Nov 23 '16 at 15:23

# C, 63 60 bytes

-3 byte for do...while loop.

i;f(n){i=n;while(--n)i*=n;do n+=i%10;while(i/=10);return n;}


Ungolfed and usage:

i;
f(n){
i=n;
while(--n)
i*=n;
do
n+=i%10;
while(i/=10);
return n;
}

main() {
printf("%d\n",f(10));
}

• Do we define f (n) as int by default? Nov 25 '16 at 14:09
• @MukulKumar this is standard in C, if there is no type then int is assumed. Nov 25 '16 at 17:14

Q/KDB+ 41 Bytes

sum{:"I"$string x}over'string prd 1+til n  Breakdown: string prd 1+til n  Get the product from 1 to n {:"I"$string x}


Function that accepts a string x and returns it converted to an integer.

over'


Iterate over the argument to it's right to the function on the left, passing each item in individually.

sum


Sum up the numbers output by the function at the end.

# PHP, 44 bytes

<?=array_sum(str_split(gmp_fact($argv[1])));  Well it's not clever, but it works. That said it turned out better than I thought (I thought I'd need to use gmp_strval() too). ## Pyke, 3 bytes (old version) SBs  Explanation: SB - product(range(1, input+1)) s - digit_sum(^)  # Groovy, 61 bytes {"${(1..it).inject{i,r->i*r}}".collect{0.parseInt(it)}.sum()​}​


Groovy doesn't even have factorial built-ins.

expr [join [split [F $n] ""] +]}  Try it online! # K (ngn/k), 10 bytes Solution: +/10\*/1+!  Try it online! Explanation to follow. # Java 10, 70 bytes A lambda from long to int. Breaks for input over 20. n->{var f=n;while(n>1)f*=--n;return(f+"").chars().map(c->c-48).sum();}  Try It Online # Java 10, 163 bytes Fully arbitrary precision. A lambda from BigInteger to BigInteger. n->{var i=n;var f=n;while(n.compareTo(n.ONE)>0)f=f.multiply(n=n.subtract(n.ONE));return(f+"").chars().mapToObj(c->i.valueOf(c-48)).reduce(n.ZERO,(a,b)->a.add(b));}  Try It Online ## Ungolfed n -> { var i = n; var f = n; while (n.compareTo(n.ONE) > 0) f = f.multiply(n = n.subtract(n.ONE)); return (f + "").chars() .mapToObj(c -> i.valueOf(c - 48)) .reduce(n.ZERO, (a, b) -> a.add(b)) ; }  # Perl 5-MList::Util=sum -MMath::BigInt -p, 43 bytes Supports arbitrarily large factorials. $_=sum(Math::BigInt->new($_)->bfac()=~/./g)  Try it online! # Perl 5-MList::Util=sum,product -p, 27 bytes Cannot handle extremely large factorials due to overflow. $_=product 1..$_;$_=sum/./g


Try it online!

# Scala, 30 bytes

1.to(_).product+""map(_-48)sum


Try it online!

And a 34 byte solution without postfix syntax:

x=>(""+1.to(x).product:\0)(_+_-48)


Try it in Scastie

# Java8 - 112 Chars

(First time here...)

String.valueOf(LongStream.rangeClosed(2,i).reduce(1,(a,b)->a*b)).chars().map(Character::getNumericValue).sum();


# Julia 0.6, 25 bytes

Julia functions are generic across input types, and generate optimized code for each combination of input types. This function works with machine sized integers or arbitrary precision integers depending on the type of the argument. eg

f(10) = 27
f(50) = -97 # overflowed
f(big(50)) = 216 # no overflow, but slower due to use of BigInt

x->sum(digits(prod(1:x)))


Try it online!

# Clojure, 65 bytes

(fn[i](apply +(map #(-(int %)48)(str(apply *(range 1(inc i)))))))


Straightforward, character \0 has integer value of 48.

(defn f [i] (->> i inc (range 1) (apply *) str (map #(- (int %) (int \0)) (apply +)))


# Pari/GP, 16 bytes

n->sumdigits(n!)


Try it online!

# Python 3, 66 bytes

import math;print(sum(map(int,str(math.factorial(int(input()))))))


It gets factorial of number, splits it into an array, then prints it!

• Welcome to PPCG! Jul 7 '18 at 13:02

# MATL, 9 bytes

:p[]&V!Us


Try it online!

MATL port of Stewie Griffin's Octave answer. Works up to n=22, same as the Octave and R answers.

:p - factorial.
[]&V - string representation of the number, fully expanded (without the []&, larger numbers use the scientific notation which won't do for our purposes).
!U transpose and convert back to numbers, getting individual digits.
s - sum those digits.

The []& part can be removed for -3 bytes, but then the range is further limited to only work up to 17, instead of 22.

I could stretch the input range to 23 with a couple of tricks, but that costs an additional 20 bytes:

:1w5X2Y%"@*t10\~?10/]][]&V!Us

(basically, use the uint64 data type instead of the usual double, multiply by each number upto the given number, but divide away 10 whenever our product is divisible by it (since trailing zeros add nothing to the digit sum).)

That seems to be as far as we can go with MATLAB/MATL without doing an ad-hoc implementation of bignum in the program.

# Vyxal, 2 bytes

‼∑


Try it Online!

A very literal interpretation of the challenge: sum_of_digits_of(factorial(input))