Generating unlimited digits of "pi" is not an easy task.
So I prompted Sonnet 3.7 (on Claude, made by Anthropic) to do it.
And it did! See code below.
This is apparently now called "Vibe coding" :)
That is because BigInt is faster than "decimal".
Vibe coding is an AI-dependent computer programming practice where a person describes a problem in a few sentences as a prompt to a large language model (LLM) tuned for coding. The LLM generates software, freeing the programmer from having to write and debug the underlying code.[1][2][3] Vibe coding is claimed by its advocates to allow even amateur programmers to produce software without the extensive training and skills which coding previously needed.[4] The term was introduced by Andrej Karpathy in February 2025
We have used Python for this task.
That worked!
AI also explained advanced algorithm used (see below)
Then I asked to compare with Google's public API where millions of digits of Pi are available
url = f"https://api.pi.delivery/v1/pi?start={start_position}&numberOfDigits={digits_to_request}"
That worked also! No errors.
Next I asked to save timestamps every 100 digits generated to a CSV file
and generate chart.
By know, you know the result, right?
Interesting find: the more digits is requested to be calculated,
the time for initial digits is increased too with this algorithm.
I.e. first 10000 digits takes 1 second when only this is requested.
When 20000 digits is calculated, first 10000 takes 2 seconds.
When 30000 digits is calculated, first 10000 takes about 3 seconds, etc.
Then we translated to JavaScript.
That worked also, but much slower.
Why?
Python "digits" package is in "native" (C) compiled code.
JavaScript "digits.js" module is a script, not native. So it is much slower, i.e. 10x
So you have it.
AI can code, at least for "known problems" like Pi digits calculation.
"In the code I provided, I'm using the Chudnovsky algorithm to calculate π. This algorithm was developed by the Chudnovsky brothers (David and Gregory) in 1989 and is one of the most efficient methods for calculating π to high precision.
The algorithm is based on a rapidly converging series that adds approximately 14 decimal digits of π per iteration, making it much more efficient than older methods like the Bailey–Borwein–Plouffe formula or Machin's formula.
The Chudnovsky algorithm uses the following formula:
1/π = 12 ∑(k=0 to ∞) [(-1)^k × (6k)! × (545140134k + 13591409)] / [(3k)! × (k!)^3 × 640320^(3k+3/2)]
In the code, you can see this implemented with variables like C, M, L, X, K, and S, which track the various parts of the calculation as we compute terms in the series.
This algorithm is notable for being used in many record-setting calculations of π, including several computations of π to trillions of digits."
No comments:
Post a Comment