n! means n × (n − 1) × ... × 3 × 2 × 1

For example, 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.

Find the sum of the digits in the number 100!


In [1]:
# Python bignum support can do this one really easily.
# Interesting things are afoot with the divisibility by 9
import math
sums = []
fs = []
by_9s = []

for i in range(1,121):
    p = sorted([int(d) for d in str(math.factorial(i))])
    f = [0 for j in range(10)]
    for j in p:
        f[j] += 1
    sums.append(sum(f))
    fs.append(f)
    by_9s.append(sum(p) / 9)
    print i, sum(p), sum(p) / 9, sum(f), "".join(map(str,p))


1 1 0 1 1
2 2 0 1 2
3 6 0 1 6
4 6 0 2 24
5 3 0 3 012
6 9 1 3 027
7 9 1 4 0045
8 9 1 5 00234
9 27 3 6 023688
10 27 3 7 0023688
11 36 4 8 00136899
12 27 3 9 000014679
13 27 3 10 0000222678
14 45 5 11 00112277889
15 45 5 13 0000133466778
16 63 7 14 00002227888899
17 63 7 15 000023455667889
18 54 6 16 0000022334567778
19 45 5 18 000000111223445688
20 54 6 19 0000000122234466789
21 63 7 20 00000001112444577999
22 72 8 22 0000000011224667777778
23 99 11 23 00000122344556667788889
24 81 9 24 000000122333334444667899
25 72 8 26 00000000011112333445558899
26 81 9 27 000000001112233444555666689
27 108 12 29 00000000011123445566678888889
28 90 10 30 000000000011113334444556678888
29 126 14 31 0000000111133344455666777889999
30 117 13 33 000000000111222233455556668888899
31 135 15 34 0000000112222222345556677778888889
32 108 12 36 000000000011111222333333356666678899
33 144 16 37 0000000011111112334445566678888888899
34 144 16 39 000000000001122223334444556666778899999
35 144 16 41 00000000011112223333333444556666666778999
36 171 19 42 000000000011112223333444556677778889999999
37 153 17 44 00000000000011112223333344455555666777889999
38 108 12 45 000000000000000011111112222222344456666677779
39 189 21 47 00000000000011122223333344455667777888888899999
40 189 21 48 000000000111111222223334444555556667777888899999
41 144 16 50 00000000000000011111122223333344455555566666677788
42 189 21 52 0000000000000111111222333444444555556666677788889999
43 180 20 53 00000000000001111222233333333344555555566666677778899
44 216 24 55 0000000000001111112222333444445555566666777788888888899
45 207 23 57 000000000000011111112222333334444555556666666677778889999
46 216 24 58 0000000000000000111122222222344455555555666678888888999999
47 225 25 60 000000000001111111111222222333334445555566666667788889999999
48 234 26 62 00000000000000111122222233333334444555555666666777777778888999
49 225 25 63 000000000000001111122222222223333334445555566666677777888888899
50 216 24 65 00000000000000000001111111222333333444444455666666666777778888899
51 198 22 67 0000000000000000001111111111222222222233333344455556666677788888999
52 279 31 68 00000000000000000011122233334444455555556666666677777777888888888999
53 279 31 70 0000000000000000011222222333333333444444455566666667777788888888999999
54 261 29 72 000000000000000000111112222222223333333344444455566677777778888888889999
55 279 31 74 00000000000000000011111222222333334444445555555555566666666677778888999999
56 333 37 75 000000000000000001111233344444444444445555566666677777777788888888888999999
57 270 30 77 00000000000000000000111111122222222233334444444455555566666667777888888899999
58 288 32 79 0000000000000000011111111112222222222333334444444455555666677778888888888999999
59 324 36 81 000000000000000000011111122222233333333444444455555666666777777788888888899999999
60 288 32 82 0000000000000000000111111112222222223333333344444444445556666666677777778888999999
61 315 35 84 000000000000000000000000001112222222222333344445555555666666777777888888888889999999
62 306 34 86 00000000000000000000111111112222222222222333333334444555555666666777777778888888999999
63 333 37 88 0000000000000000000001111111112222222233333444444445555666666666667777777788888889999999
64 324 36 90 000000000000000000001111111111112222223333333334444444444555555566666677788888888888899999
65 351 39 91 0000000000000000000011111222222222223333333444444445555555555666666667777777778888888999999
66 351 39 93 000000000000000000000001111122222222333333344444444444444445556666666677777777888888888999999
67 369 41 95 00000000000000000000001111111122222222333333444444444455555556666666666677777788888888899999999
68 342 38 97 0000000000000000000000000111111111112222222233333333344444445555555566666677777777888888899999999
69 351 39 99 000000000000000000000111111111222222222222223333333333444444444444455555666677777777888888889999999
70 459 51 101 00000000000000000000111111122222223333344444455555555556666666666677777777788888888888888999999999999
71 423 47 102 000000000000000000000000011111122222222333333344444555555566666666666777777777788888888888888888899999
72 432 48 104 00000000000000000000000111111222222222233333333444444444455566666666667777777777778888888888888889999999
73 315 35 106 0000000000000000000000000000000000011111111111111112222222233333344444445555555666666777777788888888899999
74 378 42 108 000000000000000000000001111111111111222222222222223333333333444444455555555555556666677777778888888889999999
75 432 48 110 00000000000000000000000001111111112222222223333333444444444444455555555566666666677777777788888888889999999999
76 441 49 112 0000000000000000000000001111111222222222222333333333334444444444455555555556666666677777777788888888999999999999
77 432 48 114 000000000000000000000000000000001111111222222222233333334444444444455555566666666677777778888888888888888889999999
78 423 47 116 00000000000000000000000001111111111122222222222222223333333334444444444555555555666666667777777777888888888888999999
79 441 49 117 000000000000000000000000000001111111111111112222222233333333334444555555566666666666777777777788888888888888999999999
80 450 50 119 00000000000000000000000000011111111222222222222333333334444444444444555555555555556666666666777777777888888888888899999
81 486 54 121 0000000000000000000000000001111111112222222223333333344444444444445555555555555666666667777777777777788888888999999999999
82 477 53 123 000000000000000000000000000011111111111112222233333333333333344444444444444445666666666667777777777788888888889999999999999
83 486 54 125 00000000000000000000000000000011111111112222222223333333333444444444455555555555566666666666677777777777788888888999999999999
84 477 53 127 0000000000000000000000000001111111111111112222222222223333333333333444444445555555566666666666667777777778888888888889999999999
85 414 46 129 000000000000000000000000000000000000111111111111122222222222222333333334444444444444445555555555555666666666777777788888999999999
86 495 55 131 00000000000000000000000000001111111122222222222222222333333333344444444444444555555555555566666666666777777777777788888888999999999
87 495 55 133 0000000000000000000000000001111111111111122222222222222223333333333333444444444455555555555666666677777777777777777888888889999999999
88 531 59 135 000000000000000000000000000011111111111111222222222222333333334444444444444444445555555555666666666666778888888888888888889999999999999
89 549 61 137 00000000000000000000000000000000001111111111112222222223333333333334444445555555555666666666666666777777777778888888888999999999999999999
90 585 65 139 0000000000000000000000000000111111111112222222222333333333344444444444445555555555556666666666666667777777777778888888888888899999999999999
91 594 66 141 000000000000000000000000000001111111111112222222233333344444444444455555555555555555666666666666666667777777777777777777888888888999999999999
92 540 60 143 00000000000000000000000000000111111111111122222222333333333333333333444444444444444445555555555555555566666666777777777777777888888888889999999
93 513 57 145 0000000000000000000000000000000011111111111111112222222222222222233333333333344444444444455555555555555666666666667777777777778888888888889999999
94 549 61 147 000000000000000000000000000000000000011111111111222222222223333333333334444444444445555555555555556666666666666666777777777788888888888888889999999
95 585 65 149 00000000000000000000000000000000111111112222222222222222222333333333333333444444444444555555555555666666666667777777777777778888888888999999999999999
96 648 72 150 000000000000000000000000000000000000111111111222222233333333344444444444444445555555555566666666666777777777777777888888888888888899999999999999999999
97 648 72 152 00000000000000000000000000111111111111111112222222222222333333333333444444444445555555555566666666666667777777777777777888888888888888889999999999999999
98 639 71 154 0000000000000000000000000000000000011111122222222222222223333333333444444444444444455555555566666666666666777777777777777777788888888888888889999999999999
99 648 72 156 000000000000000000000000000011111111111111122222222222222222223333333333444444444455555555555555666666666666666666677777778888888888888899999999999999999999
100 648 72 158 00000000000000000000000000000011111111111111122222222222222222223333333333444444444455555555555555666666666666666666677777778888888888888899999999999999999999
101 639 71 160 0000000000000000000000000000000000011111111112222222222222233333333333333333344444444444444555555555555566666666666666777777777777888888888888999999999999999999
102 630 70 162 000000000000000000000000000000000000011111111111122222222233333333333344444444444444444555555555555555555555555555666666666666666677777788888888888999999999999999
103 621 69 164 00000000000000000000000000000000000011111111111111112222222222222222233333333333444444444444444444555555555555555666666666667777777778888888888888888899999999999999
104 702 78 167 00000000000000000000000000000000011111111111112222222222222333333333333444444444444444455555555555555666666666667777777777777777777777777888888888888888888999999999999
105 648 72 169 0000000000000000000000000000000000000000000001111111111122222222222222333333333333333344444444444555555555556666666666666667777777777777778888888888889999999999999999999
106 639 71 171 000000000000000000000000000000000000111111111112222222222222222222333333333333333333334444444444444444444444555555556666666666666667777777777777778888888888888888889999999
107 594 66 173 00000000000000000000000000000000000000000011111111111111111112222222222222223333333333333333333333444444444445555555555555555566666666666666777777777788888888888999999999999
108 666 74 175 0000000000000000000000000000000000000000011111111111111111222222222222333333333333333344444444444444444555555555555555666666666666777777777777888888888888888888899999999999999
109 657 73 177 000000000000000000000000000000000000000111111111111122222222222222222222233333333333333333333333444444444444444445555555555666666667777777777777778888888888888888899999999999999
110 657 73 179 00000000000000000000000000000000000000111111111111111111222222222222222222222223333333333333444444444444444444555555555555566666666666677777777777777777778888888888888999999999999
111 693 77 181 0000000000000000000000000000000000000001111111111111111122222222222233333333333333344444444444444444455555555555555555555666666666666666666677777777777777777778888888888999999999999
112 765 85 183 000000000000000000000000000000000000001111111111222222222222222223333333333333333344444444444444455555555555555566666666666666777777777777777777777788888888888888888899999999999999999
113 666 74 185 00000000000000000000000000000000000111111111111111111111111112222222222222222222222223333333333333344444444444444455555555555555666666666666666667777777777777778888888888889999999999999
114 648 72 187 0000000000000000000000000000000000000000000000011111111111111111112222222222222222333333333333333333344444444444444444445555555555555555556666666666677777777888888888888888888999999999999
115 648 72 189 000000000000000000000000000000000000000000000000000111111111111111111122222222222222222233333333333333333344444444444455555555555555555555566666666677777777777788888888888889999999999999999
116 729 81 191 00000000000000000000000000000000000000000000111111111111111111111112222222222222233333333333333333344444444555555555555555566666666666666666667777777777777888888888888888889999999999999999999
117 738 82 193 0000000000000000000000000000000000000000000000011111111111111122222222222222333333333333334444444444444444444444444555555555556666666666666666666667777777777777788888888888888999999999999999999
118 756 84 195 000000000000000000000000000000000000000011111111111112222222222222222222222223333333333333333333334444444444444444455555555555555666666666666666667777777777777888888888888888888888889999999999999
119 774 86 197 00000000000000000000000000000000000000000011111111111111112222222222233333333333333333344444444444444444444444444444555555555555555556666666666667777777777777777777788888888888888888889999999999999
120 783 87 199 0000000000000000000000000000000000000000000001111111111111111111222222222222222333333333333333344444444444444455555555555555555555566666666666666667777777777788888888888888888888899999999999999999999

In [2]:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.formula.api as smf
import pandas as pd

%matplotlib inline

In [3]:
num_to_skip = 10

data = pd.DataFrame({'x': range(num_to_skip,len(by_9s)), 'y': by_9s[num_to_skip:]})
lm = smf.ols(formula='y ~ x', data=data).fit()
print lm.params

X = pd.DataFrame({'x': range(num_to_skip,len(by_9s))})
preds = lm.predict()
plt.plot(by_9s)
plt.plot(X, map(round, preds))


Intercept   -9.572473
x            0.775331
dtype: float64
Out[3]:
[<matplotlib.lines.Line2D at 0x7f1d1e571450>]

In [4]:
plt.plot(sums)


Out[4]:
[<matplotlib.lines.Line2D at 0x7f1cfd18f8d0>]

In [5]:
d = { str(i): None for i in range(10) }
for i in range(10):
    d[str(i)] = [f[i] for f in fs]

plt.plot(pd.DataFrame(d))


Out[5]:
[<matplotlib.lines.Line2D at 0x7f1cfc867a10>,
 <matplotlib.lines.Line2D at 0x7f1cfc867b90>,
 <matplotlib.lines.Line2D at 0x7f1cfc867cd0>,
 <matplotlib.lines.Line2D at 0x7f1cfc867e10>,
 <matplotlib.lines.Line2D at 0x7f1cfc867f50>,
 <matplotlib.lines.Line2D at 0x7f1cfc8730d0>,
 <matplotlib.lines.Line2D at 0x7f1cfc873210>,
 <matplotlib.lines.Line2D at 0x7f1cfc873350>,
 <matplotlib.lines.Line2D at 0x7f1cfc873490>,
 <matplotlib.lines.Line2D at 0x7f1cfc8735d0>]

In [ ]: