Skip to content

Instantly share code, notes, and snippets.

@fried
Created October 12, 2023 22:31
Show Gist options
  • Save fried/42f4ac46fb4cddfe424158aefdb131e3 to your computer and use it in GitHub Desktop.
Save fried/42f4ac46fb4cddfe424158aefdb131e3 to your computer and use it in GitHub Desktop.
Lets generate fibonacci sequence until our harddrive fills up.
#### The Fibo generator
import math
import os
def fibo(a=0, b=1):
if a == 0 and b == 1:
skip = False
else:
skip = True
if not skip:
yield a
while True:
if not skip:
yield b
else:
skip = False
a, b = b, a + b
def write_fibo_and_index(db, index, fib):
size = math.ceil(fib.bit_length()/8)
db.write(fib.to_bytes(size, "little"))
index.write(db.tell().to_bytes(5, "little"))
return size
def read_index(index):
return int.from_bytes(index.read(5), "little")
with open("fibos", "a+b") as db:
with open("index", "a+b") as index:
# figure out if we are resuming or not
last_index = os.stat(index.name).st_size - 5
if last_index < 0:
# We are starting from scatch
a,b = 0, 1
start = 0
# Write the first index
index.write((0).to_bytes(5, "little"))
else:
# Lets get a and b from the db
index.seek(last_index-10) # go back two indexes
a_start = read_index(index)
b_start = read_index(index)
next_start = read_index(index)
a_size = b_start - a_start
b_size = next_start - b_start
# Lets read a and b
db.seek(a_start)
a = int.from_bytes(db.read(a_size), "little")
b = int.from_bytes(db.read(b_size), "little")
start = index.tell()//5-1
for c, f in enumerate(fibo(a, b),start = start):
size = write_fibo_and_index(db, index, f)
print(f"Fibo {c} is {size} bytes!")
##### Reading Fibos
import sys
def read_index(index):
return int.from_bytes(index.read(5), "little")
seq = int(sys.argv[1])
sys.set_int_max_str_digits(0)
with open("index", "rb") as index:
index.seek(seq*5)
loc = read_index(index)
size = read_index(index) - loc
with open("fibos", "rb") as db:
db.seek(loc)
f = int.from_bytes(db.read(size), "little")
print(f)
@fried
Copy link
Author

fried commented Oct 12, 2023

I used 5 bytes for the index because using more than 1TB for fibo numbers is extreme :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment