Skip to content

Instantly share code, notes, and snippets.

@aciceri
Created March 14, 2026 14:03
Show Gist options
  • Select an option

  • Save aciceri/5ff4cf5283aebbaffdaa78cd85cbacde to your computer and use it in GitHub Desktop.

Select an option

Save aciceri/5ff4cf5283aebbaffdaa78cd85cbacde to your computer and use it in GitHub Desktop.
{
nixpkgs ? <nixpkgs>,
system ? builtins.currentSystem
}: let
pkgs = import nixpkgs { inherit system; };
inherit (pkgs) lib;
# Bailey–Borwein–Plouffe formula
piDigit = n: pkgs.runCommand "pi-${toString n}" {} ''
n=${toString n}; ${lib.getExe pkgs.python314} -c "
from decimal import Decimal,getcontext
getcontext().prec=$n+20
pi=sum((Decimal(4)/(8*k+1)-Decimal(2)/(8*k+4)-Decimal(1)/(8*k+5)-Decimal(1)/(8*k+6))/Decimal(16)**k for k in range($n*2+50))
print(str(pi).replace('.',\"\")[$n])
" > $out
'';
digits = 100;
pi = pkgs.runCommand "pi" {} ''
cat ${lib.concatMapStringsSep " " (n: piDigit n) (lib.range 0 digits)} | tr -d '\n' > $out
'';
in {
inherit pi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment