An anytime algorithm can be interrupted at any moment and still return its current best solution. This demo uses the Leibniz formula:
π = 4 × (1 − 1/3 + 1/5 − 1/7 + 1/9 − ...)
As more terms are computed, the approximation improves.
sum = 0
n = 0
repeat until paused:
term = (-1)^n / (2n + 1)
sum = sum + term
pi = 4 * sum
n = n + 1
return current pi