Created
July 18, 2019 17:05
-
-
Save samestep/ffb915c2a4335b973bacc8ed798eaff7 to your computer and use it in GitHub Desktop.
GEB page 73 sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bisect import bisect_right | |
positive = [] | |
negative = [] | |
# https://docs.python.org/3.7/library/bisect.html | |
def find_gt(a, x): | |
'Find leftmost value greater than x' | |
i = bisect_right(a, x) | |
if i != len(a): | |
return a[i] | |
raise ValueError | |
def next_negative(): | |
last = negative[-1] if negative else 0 | |
while positive[-1] < last: | |
next_positive() | |
next = find_gt(positive, last) | |
negative.append(last + 1 if last + 1 < next else next + 1) | |
def next_positive(): | |
last = positive[-1] | |
while len(positive) > len(negative): | |
next_negative() | |
diff = negative[len(positive)-1] | |
positive.append(last + diff) | |
def run(step): | |
positive.clear() | |
positive.append(1) | |
negative.clear() | |
while positive[-1] < 69: | |
step() | |
print('positive =', positive) | |
print('negative =', negative) | |
print('# next_positive') | |
run(next_positive) | |
print() | |
print('# next_negative') | |
run(next_negative) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment