Update all new Tasks

This commit is contained in:
Ingy döt Net 2015-02-20 09:02:09 -05:00
parent 00a190b0a6
commit 91df62d461
5697 changed files with 93386 additions and 804 deletions

View file

@ -0,0 +1,56 @@
from math import ceil, log10, factorial
def next_step(x):
result = 0
while x > 0:
result += (x % 10) ** 2
x /= 10
return result
def check(number):
candidate = 0
for n in number:
candidate = candidate * 10 + n
while candidate != 89 and candidate != 1:
candidate = next_step(candidate)
if candidate == 89:
digits_count = [0] * 10
for d in number:
digits_count[d] += 1
result = factorial(len(number))
for c in digits_count:
result /= factorial(c)
return result
return 0
def main():
limit = 100000000
cache_size = int(ceil(log10(limit)))
assert 10 ** cache_size == limit
number = [0] * cache_size
result = 0
i = cache_size - 1
while True:
if i == 0 and number[i] == 9:
break
if i == cache_size - 1 and number[i] < 9:
number[i] += 1
result += check(number)
elif number[i] == 9:
i -= 1
else:
number[i] += 1
for j in xrange(i + 1, cache_size):
number[j] = number[i]
i = cache_size - 1
result += check(number)
print result
main()

View file

@ -0,0 +1,42 @@
from itertools import combinations_with_replacement
from array import array
from time import clock
D = 8
F = [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000]
def b(n):
yield 1
for g in range(1,n+1):
gn = g
res = 0
while gn > 0:
gn,rem = divmod(gn,10)
res += rem**2
if res==89:
yield 0
else:
yield res
N = array('I',b(81*D))
for n in range(2,len(N)):
q = N[n]
while q>1:
q = N[q]
N[n] = q
es = clock()
z = 0
for n in combinations_with_replacement(range(10),D):
t = 0
for g in n:
t += g*g
if N[t] == 0:
continue
t = [0,0,0,0,0,0,0,0,0,0]
for g in n:
t[g] += 1
t1 = F[D]
for g in t:
t1 /= F[g]
z += t1
ee = clock() - es
print "\nD==" + str(D) + "\n " + str(z) + " numbers produce 1 and " + str(10**D-z) + " numbers produce 89"
print "Time ~= " + str(ee) + " secs"

View file

@ -0,0 +1,14 @@
>>> from functools import lru_cache
>>> @lru_cache(maxsize=1024)
def ids(n):
if n in {1, 89}: return n
else: return ids(sum(int(d) ** 2 for d in str(n)))
>>> ids(15)
89
>>> [ids(x) for x in range(1, 21)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1, 89]
>>> sum(ids(x) == 89 for x in range(1, 100000000))
85744333
>>>

View file

@ -0,0 +1,19 @@
>>> from functools import lru_cache
>>> @lru_cache(maxsize=1024)
def _ids(nt):
if nt in {('1',), ('8', '9')}: return nt
else: return _ids(tuple(sorted(str(sum(int(d) ** 2 for d in nt)))))
>>> def ids(n):
return int(''.join(_ids(tuple(sorted(str(n))))))
>>> ids(1), ids(15)
(1, 89)
>>> [ids(x) for x in range(1, 21)]
[1, 89, 89, 89, 89, 89, 1, 89, 89, 1, 89, 89, 1, 89, 89, 89, 89, 89, 1, 89]
>>> sum(ids(x) == 89 for x in range(1, 100000000))
85744333
>>> _ids.cache_info()
CacheInfo(hits=99991418, misses=5867462, maxsize=1024, currsize=1024)
>>>