Sum of digits of a factorial

后端 未结 10 692
囚心锁ツ
囚心锁ツ 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:31

    You have to compute the fatcorial.

    1 * 2 * 3 * 4 * 5 = 120.

    If you only want to calculate the sum of digits, you can ignore the ending zeroes.

    For 6! you can do 12 x 6 = 72 instead of 120 * 6

    For 7! you can use (72 * 7) MOD 10

    EDIT.

    I wrote a response too quickly...

    10 is the result of two prime numbers 2 and 5.

    Each time you have these 2 factors, you can ignore them.

    1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15...
    
    1   2   3   2   5   2   7   2   3    2   11    2   13    2    3
                2       3       2   3    5         2         7    5
                                2                  3
    

    The factor 5 appears at 5, 10, 15...
    Then a ending zero will appear after multiplying by 5, 10, 15...

    We have a lot of 2s and 3s... We'll overflow soon :-(

    Then, you still need a library for big numbers.

    I deserve to be downvoted!

提交回复
热议问题