Why Is √2 Hard to Compute?
√2 = 1.41421356237… — it’s irrational. Its decimal expansion never repeats and never ends. Your calculator gives you a quick answer, but only about 15 digits of it. For 1000 digits, you need a completely different approach.
Math.sqrt(2) in JavaScript and math.sqrt(2) in Python
both give you 15–16 significant decimal digits:
For our randomness test, we wanted 1000 digits — 62× more than what the built-in function can provide.
A 64-bit “double” (the standard in C, Java, JavaScript, Python’s
float) stores a number as: 1 sign bit + 11 exponent bits
+ 52 mantissa bits. Those 52 bits give you about
15–16 significant decimal digits — no matter what number you
store. √2 has infinitely many non-repeating decimal digits, so after position 16
the double just… stops. It has no more bits to spend.
Getting 1000 digits requires a completely different approach: either a library that
uses arbitrary-precision arithmetic (Python’s mpmath),
or implementing an algorithm that works in pure integers and never touches
floating-point at all (JavaScript’s BigInt).
The Math — Newton’s Method (BC Calculus level!)
You already know Newton’s Method from AP Calculus: given a function f(x), find its root by repeatedly improving your guess. Applied to √2, it produces a 3000-year-old Babylonian algorithm that is still the fastest way to compute square roots — and each step doubles the number of correct digits.
a) Newton’s Method
Newton’s Method finds a root of f(x) = 0 by starting with a guess x0 and iterating:
b) Applying Newton’s Method to √2
To find √2, we want to solve x2 − 2 = 0. So f(x) = x2 − 2 and f′(x) = 2x. Substituting into Newton’s formula:
c) Watching it Converge
Starting from x0 = 1.5 (a reasonable first guess), watch how fast Newton’s Method locks onto √2:
Python Version — mpmath
The Python approach is the “library” solution: tell mpmath how many
decimal places you want, ask for mpmath.sqrt(2), and convert the result to a
list of digits. Under the hood, mpmath uses an algorithm derived from
Newton’s Method — optimized for very large digit counts.
-
Google Colab
—
mpmathis already installed. Paste into a code cell and press Shift+Enter. -
Repl.it
— create a new Python repl, paste, run. Install
mpmathonce withpip install mpmathin the Shell tab.
JavaScript Version — BigInt + Newton’s Method
The JavaScript approach implements Newton’s Method (the Babylonian integer square root) directly, using BigInt for exact integer arithmetic. No library needed — this runs in any modern browser console or Node.js. Every step maps directly to the calculus iteration from Section 2.
Count: 1000 and
First 10: [4, 1, 4, 2, 1, 3, 5, 6, 2, 3] printed to the console,
followed by the 1000 comma-separated digits ready to paste into the
Loaded Dice Analyzer.
BigInt type (introduced in ES2020) stores integers of
arbitrary size — no upper limit. BigInt literals are written with an
n suffix: 0n, 1n, 2n,
10n ** BigInt(2020). BigInt arithmetic is always exact; unlike floating-point,
it never rounds. That’s the property we need to guarantee 1000 correct digits.
Newton’s Method Convergence — Watching the Digits Pile Up
Newton’s Method converges so fast it almost seems like cheating. Here’s the iteration table starting from x₀ = 1.5:
| Iteration | Approximate value | Correct digits |
|---|---|---|
| x₀ | 1.5 | 1 |
| x₁ | 1.4166… | 2 |
| x₂ | 1.41421356… | 8 |
| x₃ | 1.4142135623730950488… | 19 |
| x₄ | … | ∼38 |
| x₅–x₉ | … | doubles each step |
| x₁₀ | … | 1000+ |
Notice the doubling pattern in the “correct digits” column: 1 → 2 → 8 → 19 → 38 → … ≈ doubling at each step. That’s quadratic convergence, and it’s what makes Newton’s Method so powerful for square roots compared to slowly-converging series.
Precision limit: Unlimited — set
mp.dps to any valueLines of code: ~20 (excluding comments)
Educational value: See the big picture; trust the library
Where to run: Python 3, Google Colab, IDLE
Precision limit: Unlimited — BigInt integers grow as needed
Lines of code: ~60 (excluding comments)
Educational value: Understand the math; every step is visible
Where to run: Browser console (F12), Node.js
The Sanity Check — Do Your Results Agree?
Both functions should produce exactly the same 1000-digit sequence. Run both and compare the first ten digits:
Both methods produce the same result. Verify: first 10 digits after the decimal point should be [4, 1, 4, 2, 1, 3, 5, 6, 2, 3]. If yours match, Newton’s Method worked perfectly — and you just understood why a 3000-year-old Babylonian algorithm is still the fastest way to compute square roots.
Once you have the 1000-digit sequence, paste it into the Loaded Dice Analyzer (model 0–9, 1000 rolls) and run all four randomness tests. √2’s digits look random — but can they fool four statistical tests? That’s the question the Analyzing Random Integers guide explores.