Humongous Factorial Digit Sum!

Find the sum of digits of the number 100000! .(Hundred thousand)

For instance 5!=120, and sum of its digits would be 3.

You can use any programming language of your choice.

This problem is inspired from a project Euler question:

Big integers might be useful.


The answer is 1938780.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Mark Hennings
Feb 15, 2019

The Pascal code

function GetDigitSum(num: Integer): Integer;
var
bn:array[0..200000] of Integer;
st,en,car,a,b,temp,base: Integer;

begin
  base := 1000;
  for a := 0 to 200000 do bn[a] := 0;
  bn[0] := 1;
  st := 0;
  en := 0;
  for a := 2 to num do
    begin
      car := 0;
      for b := st to en do
      begin
        temp := a*bn[b] + car;
        bn[b] := temp mod base;
        car := temp div base;
      end;
      while car > 0 do
        begin
          Inc(en);
          bn[en] := car mod base;
          car := car div base;
        end;
      while bn[st] = 0 do Inc(st);
    end;
  Result := 0;
  for a := st to en do
    begin
      b := bn[a];
      while b > 0 do
        begin
          Result := Result + (b mod 10);
          b := b div 10;
        end;
    end; 
end;

does the trick (doing the calculations in base 1000 1000 , and fixing matters to use optimal integer datatypes). It calculates the value of 149346 149346 for the digit sum of 10000 ! 10000! in 0.78 0.78 s, and obtains the answer 1938780 \boxed{1938780} for the digit sum of 100000 ! 100000! in 1 1 minute 54 54 seconds.

Thanks for solution.

Amal Hari - 2 years, 3 months ago
Amal Hari
Feb 14, 2019

Solution in Java:

import java.math.BigInteger;

public class FactorialSum {

public static void main(String[] args) {
    int p=1;
    BigInteger m=new BigInteger("1");

    while(p<=100000) {
        BigInteger n= BigInteger.valueOf(p);

m=m.multiply(n);
p++;}
    String j=new String(m.toString());
    int f=j.length();
    int s=0;
    int sum=0;
    while(s<f) {
        int d=j.charAt(s)-'0';
        sum+=d;
        s++;
    }
    System.out.println(sum);


}

}

Fletcher Mattox
Aug 12, 2019
1
2
from math import factorial
print(sum(map(int,list(str(factorial(100000)))))) 

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...