# Calculating Pi via the Gregory-Leibniz series in BASIC on the VIC-20

It's Pi day tomorrow, and you know what that means!

Cherry is favorite. Apple is second favorite.

There is a well known and easy to understand method for calculating Pi with the Gregory Leibniz series. Leibniz built on the work on Gregory.

It has the added benefit of being pretty inefficient to implement on computers. This seems like a job for the Commodore VIC-20!

Our outcome repeats a variation of:

π = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15)...

The more of these you do, the more accurate the outcome.

As I've been on a retro computing kick for quite some time now, it seemed appropriate to do this on a VIC-20. I mean sure, we could do this in Rust in less than a second, but why do that when our VIC-20 can do it in HOURS.

After all, hours are MORE than seconds. So doesn't that make hours BETTER than seconds? Just an idea...

## BASIC

``````10 INPUT "ITERATIONS: ";T
20 TI\$="000000":PI=0
30 FOR I=0TOT
40 PI=PI+((4*(-1)^I)/(2*I+1))
50 NEXT
60 PRINTPI:PRINTTI
``````

The TI\$ here is not needed, it counts the number of "jiffies" on a Commodore which is approximately 1/60th of a second and is a common method to benchmark BASIC programs.

If we add a print statement to the end of line 40, we can watch it, but it's even slower.

## 64

Not to be left out, here it is on the Commodore 64 doing a million of them (in 11+ hours) in case you prefer blue on blue.

Update 3/14/2021 09:30: I just remembered this fantastic video on doing Pi on the Commodore from last year and had to rewatch it.

Update 3/16/2021: Reran the tests on the 64 with the screen disabled to check out how much the "badlines" affect the performance. The VIC20 and the 64 are now almost the same completion times. Both 10.7ish hours. The 64 was 50 minutes faster with the screen off. Those bad bad badlines :-)