Sum of digits of a factorial

后端 未结 10 688
囚心锁ツ
囚心锁ツ 2020-12-07 10:06

Link to the original problem

It\'s not a homework question. I just thought that someone might know a real solution to this problem.

I was on

相关标签:
10条回答
  • 2020-12-07 10:18

    Small, fast python script found at http://www.penjuinlabs.com/blog/?p=44. It's elegant but still brute force.

    import sys
    for arg in sys.argv[1:]:
        print reduce( lambda x,y: int(x)+int(y), 
              str( reduce( lambda x, y: x*y, range(1,int(arg)))))
    

     

    $ time python sumoffactorialdigits.py 432 951 5436 606 14 9520
    3798
    9639
    74484
    5742
    27
    141651
    
    real    0m1.252s
    user    0m1.108s
    sys     0m0.062s
    
    0 讨论(0)
  • 2020-12-07 10:23

    another solution using BigInteger

     static long q20(){
        long sum = 0;
        String factorial = factorial(new BigInteger("100")).toString();
        for(int i=0;i<factorial.length();i++){
            sum += Long.parseLong(factorial.charAt(i)+"");
        }
        return sum;
    }
    static BigInteger factorial(BigInteger n){
        BigInteger one = new BigInteger("1");
        if(n.equals(one)) return one;
        return n.multiply(factorial(n.subtract(one)));
    }
    
    0 讨论(0)
  • 2020-12-07 10:28

    I'd attack the second problem, to compute N! mod (N+1), using Wilson's theorem. That reduces the problem to testing whether N is prime.

    0 讨论(0)
  • 2020-12-07 10:29

    Let's see. We know that the calculation of n! for any reasonably-large number will eventually lead to a number with lots of trailing zeroes, which don't contribute to the sum. How about lopping off the zeroes along the way? That'd keep the sizer of the number a bit smaller?

    Hmm. Nope. I just checked, and integer overflow is still a big problem even then...

    0 讨论(0)
  • 2020-12-07 10:29

    Even without arbitrary-precision integers, this should be brute-forceable. In the problem statement you linked to, the biggest factorial that would need to be computed would be 1000!. This is a number with about 2500 digits. So just do this:

    1. Allocate an array of 3000 bytes, with each byte representing one digit in the factorial. Start with a value of 1.
    2. Run grade-school multiplication on the array repeatedly, in order to calculate the factorial.
    3. Sum the digits.

    Doing the repeated multiplications is the only potentially slow step, but I feel certain that 1000 of the multiplications could be done in a second, which is the worst case. If not, you could compute a few "milestone" values in advance and just paste them into your program.

    One potential optimization: Eliminate trailing zeros from the array when they appear. They will not affect the answer.

    OBVIOUS NOTE: I am taking a programming-competition approach here. You would probably never do this in professional work.

    0 讨论(0)
  • 2020-12-07 10:30

    1 second? Why can't you just compute n! and add up the digits? That's 10000 multiplications and no more than a few ten thousand additions, which should take approximately one zillionth of a second.

    0 讨论(0)
提交回复
热议问题