factors = map(lambda x: int( (-1)**(x-1) ) , k) maxN = 100 p_n = [0 for i in range(1+maxN)] p_n[0] = 1 p_n[1] = 1 for n in range(2, 1+maxN): i = 0 while pents[i] <= n: p_n[n] = p_n[n] + factors[i] * p_n[ n - pents[i] ] i = i + 1 N = 100 # Exclude the single partition of size n (question asks for 'sums' only) print p_n[N] - 1 # Attempt number 3, about the same speed as 2. abcdCombinatorialUtils.nPartitions.cache = {} print abcdCombinatorialUtils.nPartitions(N) - 1
# Let p(n) represent the number of different ways in which n coins can # be separated into piles. For example, five coins can separated into # piles in exactly seven different ways, so p(5)=7. # OOOOO # OOOO O # OOO OO # OOO O O # OO OO O # OO O O O # O O O O O # Find the least value of n for which p(n) is divisible by one # million. from abcdCombinatorialUtils import nPartitions nPartitions.cache = {} n = 1 while nPartitions(n) % 1000000 > 0: n += 1 print n, nPartitions(n) # 55374 and a very big number