我做了一个计算圆周率的 App I Built a Pi Calculator App

大概十几年前,装好一台新电脑,第一件事不是装游戏,而是跑 Super PI。

界面很简陋,就一个按钮。选 100 万位,点开始,然后盯着屏幕等。跑完的那个数字,就是这台机器的成绩单。我记得第一次把时间从 30 多秒压到 20 秒以内时的感觉——换了一根内存条,快了 3 秒;超频,又快了 2 秒。把硬件性能变成一个可以比较的数字,简单,直接。

后来有一天,我想知道:我手上这台 iPhone,算 100 万位要多久?

App Store 里找了一圈,没有。要么是给小朋友背圆周率的,要么只显示几十位。没有一款能让我像当年用 Super PI 那样认真跑一次——然后拿着成绩去比:你的 iPhone 15 跑了多少秒?我的快了多少?

于是自己写一个。


算法选的是 Chudnovsky——世界纪录保持者用的那个,每项能贡献约 14 位有效数字。但算法只是起点,真正决定速度的是实现:Binary Splitting 把级数运算变成大整数的分治,NTT 把大整数乘法从 O(n²) 降到 O(n log n),再加上多核并行。

我没有用现成的大数库,BigInt 从零用 Swift 写。这意味着每一个基础运算——加减乘除、取模、开方——都得自己实现,自己验证。一个 NTT 的 bug,在 1000 位时可能看不出来,但会在 100 万位的某个角落悄悄出错。花了很多时间做交叉验证,逐位对比。

并行的部分也没有想象中简单。任务拆分、内存竞争、同步开销,最终的加速比远不是"核心数倍"。能做到显著提升,已经值得。

整个开发过程中,AI 是一个重要的协作者。算法推导、Swift 实现、性能调优、UI 打磨——很多环节都有 AI 参与,开发效率提升了不少。但代码最终还是要自己理解、自己验证,AI 给的答案也并非每次都对,尤其是在大整数运算这种细节密集的地方。学会跟 AI 协作,本身也是这次开发里学到的一件事。


功能上,我选择了克制。

本可以加排行榜、社交分享。但最终只保留了我自己想用的:数字搜索(你的生日在第几位出现)、分享卡片、计算历史、关于 π 的一些故事。

还有一个细节——等待计算的进度条。invSqrt 这类操作没有中间进度,后台可能在某个阶段停滞,但进度条不能停。用户不需要知道发生了什么,他们只需要看到数字在动。


这个 App 不会是大众产品。

但如果你也曾在深夜盯着 Super PI 的进度条,等一个数字来证明自己的硬件没白买——这个 App 就是为你做的。

Super PiBench — App Store

About fifteen years ago, whenever I finished building a new PC, the first thing I ran wasn’t a game — it was Super PI.

The interface was bare: one button. Set it to one million digits, click start, and stare at the screen waiting. The number it spat out was that machine’s report card. I still remember the feeling the first time I got the time below twenty seconds — swapped a memory stick, saved three seconds; overclocked, saved two more. Turning hardware performance into a comparable number, simple and direct.

Then one day it occurred to me: how long would my iPhone take to calculate one million digits of pi?

I searched the App Store. Nothing. Either apps for children memorizing pi, or displays of a few dozen digits. Nothing that let me run a serious calculation the way Super PI did — and then compare results: your iPhone 15 took how long? Mine was faster by how much?

So I built one.


The algorithm is Chudnovsky — the one used by world record holders, contributing roughly 14 decimal digits per term. But the algorithm is just the starting point. What determines performance is the implementation: Binary Splitting turns the series summation into divide-and-conquer operations on large integers; NTT brings big integer multiplication from O(n²) down to O(n log n); and multi-core parallelism puts the A and M series chips to proper use.

I didn’t use any existing big number library. BigInt is implemented from scratch in Swift, which means every fundamental operation — addition, subtraction, multiplication, division, modulo, square root — had to be written and verified from the ground up. An NTT bug might not show up at a thousand digits but will quietly corrupt some corner of a million-digit result. A lot of time went into cross-validation, digit by digit.

Parallelism turned out to be less straightforward than expected. Task splitting, memory contention, synchronization overhead — the actual speedup is nowhere near a simple multiple of core count. Getting meaningful improvement was still satisfying.

Throughout development, AI was a significant collaborator. Algorithm derivation, Swift implementation, performance tuning, UI work — AI was involved in many stages, and the efficiency gains were real. But the code still needed to be understood and verified; AI answers aren’t always right, especially in something as detail-dense as arbitrary-precision arithmetic. Learning to work effectively with AI turned out to be one of the lessons of this project.


On features, I chose restraint.

I could have added leaderboards and social sharing. In the end I only kept what I actually wanted: digit search (what position does your birthday appear at?), a shareable result card, calculation history, and some background on π.

One detail I spent time on: the progress bar during computation. Operations like invSqrt have no intermediate progress — the backend might stall at certain stages — but the progress bar can’t. Users don’t need to know what’s happening internally. They just need to see the number moving, to know the program is running.


This app will never be a mass-market product.

But if you once stared at Super PI’s progress bar late at night, waiting for a number to justify your hardware purchase — this app was made for you.

Super PiBench — App Store

上一篇Previous 油菜花开的时候 When the Rapeseed Flowers Bloom 下一篇Next 脖子疼,所以写了个 App My Neck Hurts, So I Built an App ← 返回随笔列表← Back to Essays

留言与评论Comments

回到顶部Back to top