Skip to content

Instantly share code, notes, and snippets.

@samestep
Created July 18, 2019 17:05
Show Gist options
  • Save samestep/ffb915c2a4335b973bacc8ed798eaff7 to your computer and use it in GitHub Desktop.
Save samestep/ffb915c2a4335b973bacc8ed798eaff7 to your computer and use it in GitHub Desktop.
GEB page 73 sequence
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