#sleela 2.9 << PiGenerator.sleela -- compute Pi and stream its digits to a local file. Algorithm: the Rabinowitz-Wagon spigot. It produces the decimal digits of Pi one at a time using only bounded integer arithmetic over a single int[] array -- no arbitrary-precision/bignum type is required, which suits SLeeLa's real runtime (the widest genuine integer is 128-bit and there is no working big-integer library). Digits are written straight to the output file in fixed-size chunks via openFile/write/close, so memory stays flat instead of accumulating one enormous String. DIGITS is the number of decimal digits of Pi to compute, set to 7,000,000 as originally requested. As of the VM limit raise (SL_MAX_ARRAY_LEN 2^24 -> 2^25 and the GC byte limit 512 MiB -> 1 GiB), the ~23,333,334-cell state array a 7,000,000-digit run needs DOES now allocate (it is ~712 MiB of boxed cells). Two notes still hold (see README.md). (1) ALLOCATION is no longer the limit; main() still guards against SL_MAX_ARRAY_LEN for safety and caps rather than dying if a future target exceeds even 2^25 cells. (2) The VM is an interpreted bytecode engine with super-linear int[] element access, so a 7,000,000-digit run allocates immediately but will not COMPLETE quickly (10,000 digits ~= 48 s here; 7,000,000 is astronomically longer). The algorithm is verified correct against an independent reference at reachable sizes; lower DIGITS (1000 or 10000) to produce a finished file in seconds. >> class PiGenerator { << The digit count. Single knob for the whole program. Set to 7,000,000 as requested; the array now allocates (see header), but lower it (1000 or 10000) for a run that COMPLETES in seconds. >> int DIGITS = 7000000; < Output file name -- recognizable and digit-count stamped. > String OUT_PATH = "pi-7000000-digits.txt"; < Digits to buffer before each write() call (keeps memory flat). > int CHUNK = 4096; < Mutable streaming state, held as fields so one small helper can flush. > int fd = -1; String buf = ""; int bufLen = 0; int emitted = 0; << Hard ceiling on a single array in the VM (SL_MAX_ARRAY_LEN = 2^25 = 33,554,432). The spigot state array must not exceed this, or new int[] is refused at runtime with "array allocation failed". A 7,000,000-digit run needs 23,333,334 cells, which is under this ceiling. >> int MAX_ARRAY_LEN = 33554432; void main() { int digits = DIGITS; << The spigot needs len = floor(10 * digits / 3) + 1 cells of state; each pass over the array yields one more decimal digit of Pi. The VM refuses any single array larger than MAX_ARRAY_LEN (2^24), so if the requested digit count would exceed that, cap it to the largest count that still allocates and say so -- the run then completes instead of dying with a raw VM allocation error. >> int len = (digits * 10) / 3 + 1; if (len > MAX_ARRAY_LEN) { << Largest digits with (digits*10)/3 + 1 <= MAX_ARRAY_LEN. >> int maxDigits = ((MAX_ARRAY_LEN - 1) * 3) / 10; print("PiGenerator: requested " + digits + " digits needs an array of " + len + " cells, above the VM limit of " + MAX_ARRAY_LEN + "; capping to " + maxDigits + " digits."); digits = maxDigits; len = (digits * 10) / 3 + 1; } int[] a = new int[len]; int i = 0; while (i < len) { a[i] = 2; i = i + 1; } fd = openFile(OUT_PATH, "w"); if (fd < 0) { print("PiGenerator: could not open " + OUT_PATH + " for writing"); return; } << The spigot holds back a predigit plus a run of 9s to let carries settle: a produced value of 10 rolls the held 9s over and bumps the predigit; anything else flushes the settled predigit and its 9s. >> boolean started = false; int predigit = 0; int nines = 0; int j = 0; while (j < digits) { << One spigot pass: a base-conversion sweep from the tail of the array to the head, carrying q downward. >> int q = 0; int k = len - 1; while (k > 0) { int num = a[k] * 10 + q * (k + 1); int den = 2 * k + 1; a[k] = num % den; q = num / den; k = k - 1; } int num0 = a[0] * 10 + q; a[0] = num0 % 10; q = num0 / 10; if (q == 9) { nines = nines + 1; } else { if (q == 10) { if (started) { put(predigit + 1); int z = 0; while (z < nines) { put(0); z = z + 1; } } predigit = 0; nines = 0; } else { if (started) { put(predigit); int z = 0; while (z < nines) { put(9); z = z + 1; } } predigit = q; nines = 0; started = true; } } j = j + 1; } << Emit the final settled predigit, flush, and close. >> put(predigit); flush(); close(fd); print("PiGenerator: wrote Pi to " + OUT_PATH + " (" + emitted + " digits emitted, target " + digits + ")"); } << Append one decimal digit to the output stream. The first digit is the integer part "3", so it is written as "3." to form 3.14159...; the rest are fractional. Flushes the chunk buffer to disk when it fills. >> void put(int digit) { if (emitted == 0) { buf = buf + digit + "."; bufLen = bufLen + 2; } else { buf = buf + digit; bufLen = bufLen + 1; } emitted = emitted + 1; if (bufLen >= CHUNK) { flush(); } } << Write the buffered chunk to the file and reset the buffer. >> void flush() { if (bufLen > 0) { write(fd, buf); buf = ""; bufLen = 0; } } }