Mathematics & Computing — TNT Code Tutorial

Computing Digits of √2 — in Python and JavaScript

Two ways to extract hundreds of decimal digits of √2 — one using Python’s mpmath library, the other implementing Newton’s Method from scratch in JavaScript.

Python Version ↓ JavaScript Version ↓

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.

Why Floating-Point Has a Ceiling

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:

The Babylonian Connection
Clay tablets from ancient Babylon (around 1700 BCE) show the reciprocal of √2 computed to remarkable precision using exactly this method. The Babylonians didn’t know calculus — but they discovered Newton’s Method for square roots 3000 years before Newton.
Quadratic Convergence
If your current estimate has k correct digits, the next iteration gives you roughly 2k correct digits. 10 iterations from x₀ = 1.5 gives about 210 = 1024 correct digits. Compare: the arctangent series for π needs hundreds of terms just to gain one extra digit at a time.

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.

Python sqrt2Digits.py
    Try It In Your Browser
    No installation needed — paste the code above directly into a free cloud Python environment:
    • Google Colab — mpmath is already installed. Paste into a code cell and press Shift+Enter.
    • Repl.it — create a new Python repl, paste, run. Install mpmath once with pip install mpmath in the Shell tab.
    ℹ️ About the Copy Button: The Copy button copies from the raw JavaScript variable, not from the displayed panel. This ensures no HTML markup, keyword-highlight spans, or HTML entity characters are included — you get exactly the Python source text. Paste directly into your editor or Colab cell.

    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.

    JavaScript sqrt2Digits.js
      Try It In Your Browser — Right Now
      Zero installation required. Open any browser, press F12 (or right-click → Inspect), click the Console tab, paste the entire code above, and press Enter. You should see 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.
      ℹ️ About BigInt: JavaScript’s 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.

      Python — mpmath
      Approach: Library does the work (Newton-based algorithm)
      Precision limit: Unlimited — set mp.dps to any value
      Lines of code: ~20 (excluding comments)
      Educational value: See the big picture; trust the library
      Where to run: Python 3, Google Colab, IDLE
      JavaScript — BigInt
      Approach: You implement Newton’s Method (integer isqrt)
      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.