import json from datetime import datetime from struct import pack from zoneinfo import ZoneInfo import numpy as np NUM_DIGITS = 3144 ARRAY_SIZE = NUM_DIGITS * 10 // 3 + 1 TZ = 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 = 0 max_quotient = 0 max_remainder = 0 print(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}")