How can I calculate pi using Bash command
Asked Answered
S

4

19

I am learning bash scripting. While exploring the math functions i am came across a command which calculated the value of pi.

seq -f '4/%g' 1 2 99999 | paste -sd-+ | bc -l

Although i understand how the basic seq command works, I am unable to understand how does the above command works. Can anybody please clarify how does it work.?

Strohbehn answered 7/5, 2014 at 17:45 Comment(5)
Why can it not be as simple as this: bc -l <<< 'scale=5; 22/7'Limner
Check on Wiki section named Continued fractions.Ilan
@Limner Sorry, but π is not 22/7.Mimosa
Sure I know that but even this seq is also approximation albeit a better one no doubt.Limner
Related: superuser.com/questions/275516/…Dasteel
M
25

This calculates the value of π using Gregory–Leibniz series:

enter image description here

seq -f '4/%g' 1 2 99999 generates the fractions:

4/1
4/3
4/5
4/7
4/9
4/11
4/13
4/15
4/17
4/19

The paste pipeline paste -sd-+ combines those with alternate delimiters - and +.

Finally, bc -l performs the arithmetic to give the result.


EDIT: As noted in the comment, this sequence converges very slowly. Machin's formula has a significantly higher rate of convergence:

enter image description here

Using the same expansion for tan-1(x):

enter image description here

to compute π, we can see that it produces the correct value to 50 digits1 using just the first 50 terms of the series:

$ { echo -n "scale=50;"; seq 1 2 100 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l
3.14159265358979323846264338327950288419716939937510

With just 100 terms, the value of π is computed accurately to more than 100 digits:

$ { echo -n "scale=100;"; seq 1 2 200 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

1 Pi

Mimosa answered 7/5, 2014 at 17:51 Comment(2)
And that sequence converges very slowly. You need over 1500 iterations to get an approximation better than 22/7.Thresathresh
@KeithThompson You're correct. The edit computes π using Machin's formula which converges much faster.Mimosa
F
20

Not a direct answer to your question about using seq, but pi can be easily computed using bc:

 echo "scale=1000; 4*a(1)" | bc -l

a is arctan, and this give pi to 1000 digits.

Florinda answered 1/5, 2015 at 14:30 Comment(3)
You could pipe sed 's/.$//' at the end and change it to scale=1001 because the last digit gets fudged but I still like this answer.Penknife
@Penknife or also you can pipe xargs for the same reason of getting rid of ``'s at the EOLCheckers
I would also add BC_LINE_LENGTH=0 bc -l to avoid the line breaking madness: unix.stackexchange.com/questions/365510/…Dasteel
M
7
seq -f 4 %g 1 2 99999 

Gives the data:

4/1
4/3 
4/5 
...
4/9999

The paste command takes this list and inserts a - between the first two, a + between the second two, etc (and puts it on one line, so):

4/1-4/3+4/5-4/7......4/9999

Which is an approximation of pi. The 'bc' program calculates this and prints the value.

Montelongo answered 7/5, 2014 at 17:51 Comment(0)
D
-1

For when you really need some of those sweet pi digits:

sudo apt install pi
pi 10000000

Benchmark vs echo "scale=10000; 4*a(1)" | BC_LINE_LENGTH=0 bc -l with time <command>:

digits pi bc
10^3 0.24s
10^4 6ms 87s
10^5 60ms
10^6 0.8s
10^7 14s

The pi command is a demo of the CNL C++ arbitrary precision library: https://www.ginac.de/CLN/

Tested on Ubuntu 22.04, Lenovo ThinkPad P51.

Dasteel answered 17/12, 2022 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.