-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition_num.py
58 lines (41 loc) · 2.07 KB
/
partition_num.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
55
56
def intsfrom1(): # gens ints starting at 1
n = 1
while True:
yield n
n = n + 1
def oddsfrom3(): # gens odds starting at 3
n = 3
while True:
yield n
n = n+2
def tochange(): # gens the sequence of what numbers before to add/subtract
ints = intsfrom1()
odds = oddsfrom3()
current = 1
yield current
while True: # builds from the difference being the sequence of ints and odds
current = current + next(ints)
yield current
current = current + next(odds)
yield current
partiton_seq = [1] # sequene of partition numbers
end = 1000 # how many iterations before stopping, since we already have 1 term, this is the number of extra terms, so you will get up to the end+1 term in the list
for iteration in range(end): # interates through the last nth number in the list, adding and subtracting to find the next term, then appends that term to the list
seq_len = len(partiton_seq)
counter = 0 # counts how many added
add = True # true when adding, false when subtracting
next_term = 0 # variable to store the next term, generated by adding/subtracting previous values
for n in tochange(): # tochange gives what numbers to add/subtract to find the next term, this goes through all of those numbers and adds/subtracts
# twice, then changes, until the list is too small for the next one to add/subtract, then stops
if n > seq_len:
break # stops if seqence isn't long enough
if counter == 2:
add = not add # flips add/subtract after 2 iterations of adding/subtracting
counter = 0
if add:
next_term = next_term + partiton_seq[-n] # adds the nth last in the seqence, eg: -1 is the last
else:
next_term = next_term - partiton_seq[-n] # subtracts the nth last in the seqence, eg: -1 is the last
counter = counter+1 # increments the counter
partiton_seq.append(next_term) # adds the next term to the sequence
print(partiton_seq[:30]) # prints the first 30 partition numbers