-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplosiveSum.py
More file actions
38 lines (30 loc) · 1.02 KB
/
ExplosiveSum.py
File metadata and controls
38 lines (30 loc) · 1.02 KB
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
# Dictionary of already computed partitions
partitions = {}
# Define key
def hash(a, b):
return "".join([str(a), " ", str(b)])
# Recursive partition function
def partition(sum, largest_number):
# We cannot go further, no number left
if largest_number == 0:
return 0
# We managed to find a solution : +1
if 0 == sum:
return 1
# The number substracted is too big : not a solution
if 0 > sum:
return 0
# If it is already computed, no recursive call
if hash(sum, largest_number) in partitions:
return partitions[hash(sum, largest_number)]
# Recursive call
partitions[hash(sum, largest_number)] = partition(sum, largest_number - 1) + partition(sum - largest_number,
largest_number)
return partitions[hash(sum, largest_number)]
# Computation function
def exp_sum(n):
if n < 0:
return 0
if n != 0:
return 1 + partition(n, n - 1)
return 1