User avatar
cnxsoft
Posts: 191
Joined: Sat Oct 15, 2011 2:33 pm
Location: Chiang Mai, Thailand

Calculate Pi in the Command Line

Wed May 16, 2012 2:21 pm

I've just come across a post showing how you can calculate Pi in a shell script
http://www.tux-planet.fr/calculer-le-ch ... ous-linux/ (in French),

Code: Select all

echo "scale=10; 4*a(1)" | bc -l
It's using the Arc Tangent function a() and bc utils. scale gives you the number of decimals.
The record is 5 trillions decimals by American and Japanese researcher. Setting scale to 10,000 takes almost 2 minutes in an Intel Core i7.

It's useless, but I still find it's neat!

kreotak
Posts: 2
Joined: Sat Jul 28, 2012 6:14 pm

Re: Calculate Pi in the Command Line

Sat Jul 28, 2012 6:17 pm

The RPi took around 13 seconds to calculate 1500 digits, my i7 3770k took 1.2 seconds, my i5 2100 took 3.5 seconds on 1,2 ghz.

The performance of the Pi isn´t so bad considering that i haven´t even overclocked it yet...YET :twisted:

The only problem right now is the 5V power output over GPIO pins, all my fans refuse to spin at a decent speed to cool it once i overclock it

User avatar
megazirt
Posts: 9
Joined: Tue Oct 09, 2012 1:49 am
Location: Thetford, Norfolk, UK

Re: Calculate Pi in the Command Line

Thu Oct 11, 2012 1:28 am

You can just add the "time" command to the front of the line to get a more accurate reading,
time echo "scale=1500; 4*a(1)" | bc -l
You will get a line at the end, after the Pi number, starting "real" which gives you the execution time.
The laptop I used this on gave me "0m4.057s", my server "0m2.766s" and the interesting bit is my Pi - 0m11.261s

This still isn't a bullet proof way of comparing processing speeds, the execution time will vary a little on each run, but it allows some bragging rights for over-clockers. :P

User avatar
fos
Posts: 104
Joined: Wed Nov 16, 2011 1:48 am
Location: Texas & Kansas, United States

Re: Calculate Pi in the Command Line

Thu Oct 11, 2012 2:35 am

real 0m1.175s

On my Mac Book Pro - Core i7 Quad

;)

Couldn't resist. My RPi is still way cool!
https://faroutscience.com

User avatar
rpdom
Posts: 21536
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Calculate Pi in the Command Line

Thu Oct 11, 2012 1:13 pm

Just for a laugh, I tried this on my old P1MMX-166 laptop: real 1m11.792s

On my Pi: real 0m9.631s :-)

canibalimao
Posts: 557
Joined: Tue Aug 07, 2012 8:48 pm
Location: Portugal

Re: Calculate Pi in the Command Line

Thu Oct 11, 2012 5:53 pm

I've got this "score" on my RPi:

Code: Select all

real    0m11.009s
Not so bad :D

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: Calculate Pi in the Command Line

Thu Oct 11, 2012 7:32 pm

root@raspberrypi:~# time echo "scale=1500; 4*a(1)" | bc -l
-bash: bc: command not found

it doesn't work, am I missing something here??

Richard S.

User avatar
megazirt
Posts: 9
Joined: Tue Oct 09, 2012 1:49 am
Location: Thetford, Norfolk, UK

Re: Calculate Pi in the Command Line

Thu Oct 11, 2012 8:14 pm

redhawk wrote:root@raspberrypi:~# time echo "scale=1500; 4*a(1)" | bc -l
-bash: bc: command not found

it doesn't work, am I missing something here??

Richard S.
Like it says, your missing the "bc" command. It's a bit off topic to tell you how to install it, but it'll be something simple like "yum install bc" or "apt-get install bc", depending on what package manager your operating system uses. It's a small download, 192k on Redsleeve.

hotcayle
Posts: 1
Joined: Mon Feb 04, 2013 3:06 am

Re: Calculate Pi in the Command Line

Fri Feb 15, 2013 5:22 am

# to install bc simply type in

Code: Select all

sudo apt-get install bc
this is the code I ran on my RPI last night ;)

Code: Select all

time echo "scale=1500; 4*a(1)" | bc -l
real 0m9.995s
user 0m9.920s
sys 0m0.020s

now how the heck do I get my new user for pi to have the cool colored cmd prompt that's easy on the eyes!!!????

gio74
Posts: 11
Joined: Sat Jan 02, 2016 2:27 pm

Re: Calculate Pi in the Command Line

Thu Mar 17, 2016 8:24 am

I'd like to display one digit after another while it calculates, is it possible?

Heater
Posts: 19326
Joined: Tue Jul 17, 2012 3:02 pm

Re: Calculate Pi in the Command Line

Thu Mar 17, 2016 1:10 pm

No, I don't think the algorithms used create one digit at a time.

On Pi day I set my Pi calculating 100 million digits of Pi. It took about an hour:
viewtopic.php?f=63&t=72055
Slava Ukrayini.

User avatar
megazirt
Posts: 9
Joined: Tue Oct 09, 2012 1:49 am
Location: Thetford, Norfolk, UK

Re: Calculate Pi in the Command Line

Fri Mar 18, 2016 1:53 am

I guess it would be possible to loop the line and generate Pi at increasing length (scale=i) of decimal places (plus one) and keep updating with the last decimal place (minus one) and use something like "tail" for the output but you would have to recalculate Pi repeatedly over and over and it would be a very inefficient way of doing it.

I've no idea if someone has made a dedicated program that would do such a thing as updating per decimal, a quick Google didn't find anything.

Heater
Posts: 19326
Joined: Tue Jul 17, 2012 3:02 pm

Re: Calculate Pi in the Command Line

Fri Mar 18, 2016 5:48 am

megazirt ,
I guess it would be possible to loop the line and generate Pi at increasing length (scale=i) of decimal places (plus one) and keep updating with the last decimal place (minus one)
You could. and would take longer and longer to run for each extra digit until very soon you would wonder why you are doing it because nothing is happening.

Interestingly there is a way to calculate the Nth digit of Pi without having to calculate all the digits befor it first : https://en.wikipedia.org/wiki/Bailey%E2 ... for_.CF.80

So you could in fact use that to calculate the first digit, then the second, then the third...ad infinitum.

Only slight hitch is that it produces the digits of Pi in hexadecimal.
Slava Ukrayini.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest

Re: Calculate Pi in the Command Line

Fri Mar 18, 2016 12:25 pm

If only there was some way to convert hexadecimal numbers into decimal ... Damn! :lol:

ghans
Posts: 7893
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Calculate Pi in the Command Line

Fri Mar 18, 2016 1:02 pm

Use the spigot algorithm for Pi proposed by Rabinowitz and
Wagon :

http://www.jjj.de/hfloat/spigot.haenel.txt


ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Heater
Posts: 19326
Joined: Tue Jul 17, 2012 3:02 pm

Re: Calculate Pi in the Command Line

Fri Mar 18, 2016 1:35 pm

morphy_richards
If only there was some way to convert hexadecimal numbers into decimal ... Damn!
Yes easy but, when you have are talking about the billionth digit of Pi in hex being given without any of the other digits how are you going to convert that to the decimal digit of the right position in Pi?

There is a program to compute the n'th decimal digit of Pi here: http://stackoverflow.com/questions/5905 ... igit-of-pi But it seems to be a lot slower than just using gmp-chudnovsky to calculate all the digits then throw them all away except the last. Except of course it does not need gigabytes of RAM to run in.
Slava Ukrayini.

User avatar
morphy_richards
Posts: 1603
Joined: Mon Mar 05, 2012 3:26 pm
Location: Epping Forest

Re: Calculate Pi in the Command Line

Fri Mar 18, 2016 1:54 pm

Heater wrote: Yes easy but, when you have are talking about the billionth digit of Pi in hex being given without any of the other digits how are you going to convert that to the decimal digit of the right position in Pi?
aahhhhhhhh ... yes, I see that is quite fiendish.

User avatar
buja
Posts: 626
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Calculate Pi in the Command Line

Sat Mar 19, 2016 11:15 am

On my Pi 3:

Code: Select all

time echo "scale=1500; 4*a(1)" | bc -l
3.1415926....

real	0m4.336s
user	0m4.280s
sys	0m0.020s
Oh, I had to install bc first, it's not installed by default (Raspbian jessie).

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN

Re: Calculate Pi in the Command Line

Tue Apr 12, 2016 3:12 am

Its been a while since anyone posted here, so, I'm gonna start it up again... with some additional insights and perhaps some more fun...

First, there are many algorithms that will give you PI which converge faster than 4 * atan(1); this one, for instance:

16 * atan(1/5) - 4 * atan(1/239)

Try it in bc...

Secondly, there is a better way to get PI from the command line (faster, mostly) using python, and the decimal module. A couple of years ago I wrote a couple of py modules which add transcendental functionality to the decimal module of python (it is fast!)

pdeclib.py and pilib.py provide not only transcendental functions to python decimal, but also provide several algorithms for calculating PI from the command line using pure python.

This is the link for pdeclib & pilib on PyPi.

I have noticed that on the PI 3B this still runs fast using Python2... but, is slower (big fat surprise) in Python3... which is just the opposite of what it should be... something is wrong upstream (still looking into it).

Enjoy
marcus
:ugeek:

NewMC
Posts: 3
Joined: Tue Oct 18, 2016 2:00 pm

Re: Calculate Pi in the Command Line

Tue Oct 18, 2016 2:02 pm

real 0m4.206s
user 0m4.200s
sys 0m0.010s

Command from OP on RPi 3 running Jessie Lite.

NewMC
Posts: 3
Joined: Tue Oct 18, 2016 2:00 pm

Re: Calculate Pi in the Command Line

Tue Oct 18, 2016 2:24 pm

MarkHaysHarris777 wrote:Its been a while since anyone posted here, so, I'm gonna start it up again... with some additional insights and perhaps some more fun...

First, there are many algorithms that will give you PI which converge faster than 4 * atan(1); this one, for instance:

16 * atan(1/5) - 4 * atan(1/239)

Try it in bc...

Secondly, there is a better way to get PI from the command line (faster, mostly) using python, and the decimal module. A couple of years ago I wrote a couple of py modules which add transcendental functionality to the decimal module of python (it is fast!)

pdeclib.py and pilib.py provide not only transcendental functions to python decimal, but also provide several algorithms for calculating PI from the command line using pure python.

This is the link for pdeclib & pilib on PyPi.

I have noticed that on the PI 3B this still runs fast using Python2... but, is slower (big fat surprise) in Python3... which is just the opposite of what it should be... something is wrong upstream (still looking into it).

Enjoy
time echo "16*a(1/5)-4*a(1/239)" | bc -l
3.14159265358979323848

real 0m0.012s
user 0m0.000s
sys 0m0.000s

RPi 3 w/ Jessie Lite

David613
Posts: 57
Joined: Tue Mar 08, 2016 5:20 am

Re: Calculate Pi in the Command Line

Wed Oct 26, 2016 11:45 pm

Just because I had to try, with scale=1500; 4*a(1)
Linux mint 17.2 AMD X4 860K
real 0m1.116s

And Raspbian pi zero
real 0m7.951s

David613
Posts: 57
Joined: Tue Mar 08, 2016 5:20 am

Re: Calculate Pi in the Command Line

Thu Oct 27, 2016 12:42 am

And because I'm avoiding my calculus homework, scale=1500; 16*a(1/5)-4*a(1/239)
Linux mint 17.2 AMD X4 860K
real 0m0. 424s

And Raspbian pi zero
real 0m3.271s

scale=15000; 16*a(1/5)-4*a(1/239)
Linux mint 17.2 AMD X4 860K
real 1m45.601s

Return to “Off topic discussion”