Something went wrong. Try again.
A way to compute 3141 digits of pi on the very old Z80 CPU.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import jsonfrom datetime import datetimefrom struct import packfrom zoneinfo import ZoneInfo
import numpy as np
NUM_DIGITS = 3144ARRAY_SIZE = NUM_DIGITS * 10 // 3 + 1TZ = ZoneInfo("Europe/Paris")print("Running Spigot Algorithm for pi with:")print("\t Base: 10_000")print(f"\t Digits number: {NUM_DIGITS}")print(f"\t Array length: {ARRAY_SIZE}")print()
start_time = datetime.now(tz=TZ).strftime("%d/%m/%Y %H:%M:%S")
np.set_printoptions(formatter={"int":hex})array = np.array([2] * ARRAY_SIZE)max_carry = 0max_quotient = 0max_remainder = 0print(f"Step 0/{NUM_DIGITS // 4}:")print("digits = 0000, predigits = 0000, 9999s = 0")print(array)print()
def main(f): global max_carry, max_quotient, max_remainder for step in range(1, NUM_DIGITS // 4 + 1): carry = 0 for i in range(len(array)-1,0,-1): x = 10_000 * array[i] + carry q = x // (2*i+1) r = x % (2 * i + 1) max_quotient = max(q, max_quotient) max_remainder = max(r, max_remainder) array[i] = r carry = (x // (2*i+1)) * i max_carry = max(carry, max_carry) x0 = 10_000 * array[0] + carry array[0] = x0 % 10_000 p = x0 // 10_000
print(f"Step {step}/{NUM_DIGITS // 4 + 1}:") print(f"digits = {p:04d}") f.write(pack(">H", p))
# print(array) print()
with open('pi-mock.json', 'w') as f: end_time = datetime.now(tz=ZoneInfo("Europe/Paris")).strftime("%d/%m/%Y %H:%M:%S") json.dump({ "start_ts": start_time, "end_ts": end_time, }, f)with open('pi-mock.bin', 'wb') as f: main(f)
print(f"Max carry: {max_carry}, Max quotient: {max_quotient}, Max remainder: {max_remainder}")