-
Notifications
You must be signed in to change notification settings - Fork 172
/
Integer_Partition_Function.py
54 lines (40 loc) · 1.26 KB
/
Integer_Partition_Function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Code by BH4
import math
def memorize(f):
"""Simple memorization decorator.
Defines a dictionary which is then checked if an entry exists for any
argument which the function being decorated is called on. If it
already exists then that value is returned without calling the function,
if it does not exist then the function is called to determine the output
and it is stored in the dictionary.
"""
mem = {}
def helper(n):
if n not in mem:
mem[n] = f(n)
return mem[n]
return helper
@memorize
def num_integer_partitions(n):
"""Returns the number of ways n can be written in terms of a sum of
positive integers.
Uses memorization to speed up calculation.
"""
if n == 0 or n == 1:
return 1
if n < 0:
return 0
# Recurrence relation for the partition function.
tot = 0
maxK = int((1+math.sqrt(1+24*n))/6)
for k in range(1, maxK+1):
a = n-k*(3*k-1)//2
b = n-k*(3*k+1)//2
if k % 2 == 0:
tot -= num_integer_partitions(a)+num_integer_partitions(b)
else:
tot += num_integer_partitions(a)+num_integer_partitions(b)
return tot
# Example of use
for i in range(0, 20):
print(num_integer_partitions(i))