eph baum dot dev

← Back to blog

Condoning Another Pi Day

Published on 11/25/2023 01:34 PM by Eph Baum

Featured Image

11/24 wasn’t just Black Friday. It became another Pi day around here.

We had pie for breakfast, leftover from Thanksgiving. We had Quiche, an egg pie, for lunch. Finally, for dinner, we had ourselves some pizza pie from a local woodfire place that was delicious. For dessert, yep, more pie.

Pie day needed to be condoned, however.

1124

Those numbers definitely appear in Pi. I posit that all numeric sequence appear in Pi somewhere due to the nature of infinity and all.

I cheated and found them in a couple of sources that store the value of Pi to millions of places and doing a simple text search

It’s there.

But where?

I guessed it was in the first 10k to 100k places.

But I wanted to tell you, dear reader, that it was Pi day due to 1124 appearing at position n.

Calculating Pi?

Could I calculate that myself?

I could use the Chudnovsky algorithm.

There’s a python script in the Wiki page:

import decimal

def binary_split(a, b):
    if b == a + 1:
        Pab = -(6*a - 5)*(2*a - 1)*(6*a - 1)
        Qab = 10939058860032000 * a**3
        Rab = Pab * (545140134*a + 13591409)
    else:
        m = (a + b) // 2
        Pam, Qam, Ram = binary_split(a, m)
        Pmb, Qmb, Rmb = binary_split(m, b)
        
        Pab = Pam * Pmb
        Qab = Qam * Qmb
        Rab = Qmb * Ram + Pam * Rmb
    return Pab, Qab, Rab

def chudnovsky(n):
    P1n, Q1n, R1n = binary_split(1, n)
    return (426880 * decimal.Decimal(10005).sqrt() * Q1n) / (13591409*Q1n + R1n)

print(chudnovsky(2))  # 3.141592653589793238462643384

So, of course I could.

This method wouldn’t yield the precision I need.

You could do something like this to get to a certain precision:

from decimal import Decimal, getcontext
import math

def calculate_pi(precision):
"""
Calculate pi using the Chudnovsky algorithm to the specified precision.
"""
getcontext().prec = precision + 1  # Set precision
C = 426880 * Decimal(math.sqrt(10005))
M = 1
L = 13591409
X = 1
K = 6
S = L

for i in range(1, precision):
    M = (M * (K ** 3 - 16 * K)) // i ** 3 
    L += 545140134
    X *= -262537412640768000
    S += Decimal(M * L) / X
    K += 12

pi = C / S
return str(pi)[:precision]

# Calculate Pi to 100 digits
pi_100_digits = calculate_pi(100)
pi_100_digits

pi_100000_digits = calculate_pi(100000)
pi_100000_digits

It’s not the most efficient solution. I think I might have made repl.it mad when I tried to get 100,000 digits with the above. I gave up trying to even get to 10,000 digits in other 20 minutes of waiting.

It’s obviously not efficient - just look at it!

So, that didn’t seem like an easy enough path and I really didn’t want to spend all that much time / money on solving this problem. Also, I’m not a Python expert by any means.

Search a Pre-calculated Dataset

There are sources of this data around the internet, one need only do a little searching.

A couple of examples can be found here and here, so, easy enough, I have me some pi to search.

To find the digits with an existing dataset I figured I could:

So, back to repl.it I went looking to find the sequence in a pre-calculated dataset, like a chump who was too lazy to calculate Pi. Who has time to calculate Pi? I know it can be calculated but others have already done so, so I guess that’s good enough for me.

My first attempt was to whip up something in Rust because I’m always looking for excuses to use Rust. At first I used reqwest and tokio but, unfortunately, that resulted in some OpenSSL issues due to the dependency on OpenSSL that Repl.it didn’t meet and, so, I opted for a least resistant path and switched to a more simplistic blocking solution by using ureq and boom💥:

Sequence found at position: 25705

The code is simple:

fn find_sequence_in_url(url: &str, sequence: &str) -> Result<Option<usize>, Box<dyn std::error::Error>> {
    let response = ureq::get(url).call()?.into_string()?;

    Ok(response.find(sequence))
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = "https://www.mathsisfun.com/numbers/images/pi-million.txt";
    let sequence = "1124";

    match find_sequence_in_url(url, sequence)? {
        Some(position) => println!("Sequence found at position: {}", position),
        None => println!("Sequence not found"),
    }

    Ok(())
}

But it’s effective enough.

So, now I know that yesterday was another Pi day and that pattern appears only 25705 places into to Pi.

So, literally every day can be Pi day!

Enjoy your Pie!

Written by Eph Baum

  • Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    Making Brutalist Design Accessible: A Journey in WCAG AA Compliance

    How I transformed my brutalist blog theme to meet WCAG AA accessibility standards while preserving its vibrant, random aesthetic. Talking about contrast ratios, color theory, and inclusive design.

  • Building Horror Movie Season: A Journey in AI-Augmented Development

    Building Horror Movie Season: A Journey in AI-Augmented Development

    How I built a production web app primarily through 'vibe coding' with Claude, and what it taught me about the future of software development. A deep dive into AI-augmented development, the Horror Movie Season app, and reflections on the evolving role of engineers in the age of LLMs.

  • Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering: Building Resiliency in Ourselves and Our Systems

    Chaos Engineering isn't just about breaking systems — it's about building resilient teams, processes, and cultures. Learn how deliberate practice strengthens both technical and human architecture, and discover "Eph's Law": If a single engineer can bring down production, the failure isn't theirs — it's the process.

  • Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    Using LLMs to Audit and Clean Up Your Codebase: A Real-World Example

    How I used an LLM to systematically audit and remove 228 unused image files from my legacy dev blog repository, saving hours of manual work and demonstrating the practical value of AI-assisted development.

  • Migrating from Ghost CMS to Astro: A Complete Journey

    Migrating from Ghost CMS to Astro: A Complete Journey

    The complete 2-year journey of migrating from Ghost CMS to Astro—from initial script development in October 2023 to final completion in October 2025. Documents the blog's 11-year evolution, custom backup conversion script, image restoration process, and the intensive 4-day development sprint. Includes honest insights about how a few days of actual work got spread across two years due to life priorities.

  • 50 Stars - Puzzle Solver (of Little Renown)

    50 Stars - Puzzle Solver (of Little Renown)

    From coding puzzle dropout to 50-star champion—discover how AI became the ultimate coding partner for completing Advent of Code 2023. A celebration of persistence, imposter syndrome, and the surprising ways generative AI can help you level up your problem-solving game.