def fund(n): lower, upper = bounds(n) p = 0 for b in range(lower, upper+1): r = n - b for seq in mtools.comb(range(1,n+1), r): a = mtools.mul(seq) p += a return mtools.fac(n+1)/p
def facs(lim): l = [] i = 1 k = 1 while k < lim: l.append(k) i = i + 1 k = mtools.fac(i) l.append(k) return l
def c(n, r): a = mtools.fac(r) b = 1 for i in range(n-r+1,n+1): b = b*i return b/a
import math, time, mtools, sys fac_tab = [mtools.fac(x) for x in range(10)] chain_tab = {} def solve(): cnt = 0 for i in range(1, 1000000): n = chain(i) chain_tab[i] = n if n == 60: cnt = cnt + 1 return cnt def sum_fac(n): l = mtools.separate_digits(n) l2 = [fac_tab[x] for x in l] return sum(l2) def chain(i): c = 1 d = {i:True} while True: v = sum_fac(i) if chain_tab.has_key(v): return c + chain_tab[v] if d.has_key(v): return c d[v] = True c = c + 1 i = v
''' 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are equal to the sum of the factorial of their digits. Note: as 1! = 1 and 2! = 2 are not sums they are not included. ''' import time, mtools tab = [mtools.fac(x) for x in range(0, 10)] def is_curious(n): global tab l = get_digits(n) l2 = [tab[x] for x in l] return n == sum(l2) def get_digits(n): l = [] while n > 0: l.append(n%10) n = n /10 l.reverse() return l def test(): assert [1, 2, 3] == get_digits(123) assert is_curious(145) def solve():