Monday, 12 March 2018

3.141592653589793238462643383279

Happy Pi day everyone. March 14th is Pi day because 3.14.



I created a BASH script to test the calculation in this video.

It calculates Pi without pre-known values but by adding and subtracting fractions of one over odd numbers.

1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ...

It gets more accurate the longer you let it run but only for a short time as fractional math in BASH is limited.



#!/bin/bash

D=1
P=0
i=1
while true; do
 O="-"
 (( i++ % 2 )) && O="+"
 P=$(echo "scale=66; ${P} ${O} 1/${D}" | bc)
 printf "%0.20f               \r" $(echo "scale=21; ${P} * 4" | bc)
 ((D+=2))
done