Calculate a transcendental number (pi)

seq 1 2 99999999 | sed 's!^!4/!' | paste -sd-+ | bc -l
Calculate pi from the infinite series 4/1 - 4/3 + 4/5 - 4/7 + ... This expansion was formulated by Gottfried Leibniz: http://en.wikipedia.org/wiki/Leibniz_formula_for_pi I helped rubenmoran create the sum of a sequence of numbers and he replied with a command for the sequence: 1 + 2 -3 + 4 ... This set me thinking. Transcendental numbers! seq provides the odd numbers 1, 3, 5 sed turns them into 4/1 4/3 4/5 paste inserts - and + bc -l does the calculation Note: 100 million iterations takes quite a while. 1 billion and I run out of memory.
Sample Output
$ seq 1 2 99999999 | sed 's!^!4/!' | paste -sd-+ | bc -l
3.14159263358979321063

0
By: flatcap
2011-02-09 23:36:07

These Might Interest You

  • Change the scale to adjust number of decimal places prefix the command with "time" to benchmark the computer (compare how long it takes to calculate 10000 digits of pi on various computers). Show Sample Output


    0
    echo "scale=1000; 4*a(1)" | bc -l
    regulatre · 2014-07-31 19:13:26 0
  • calculate if "$1" is a number ... decimals included :)


    0
    (($1 > 0)) && echo "var is a number"
    khayyam · 2013-03-22 01:15:00 0

  • 0
    expr `echo "123671" | sed -e 's/[0-9]/ + &/g' -e 's/^ +//g'` 20
    maher · 2012-06-24 23:12:30 0
  • shell order of operation example which calculates: x = number of physical CPU's y = number of cores per CPU 2(x * y) + 1 = CPU load limit Show Sample Output


    0
    echo "$(( $(( $(grep 'physical id' '/proc/cpuinfo' | uniq | wc -l) * $(grep 'core id' '/proc/cpuinfo' | wc -l) )) * 2 + 1 ))"
    snorf · 2015-11-14 20:44:39 1

What Others Think

Who needs all this math crap on the command line?
bashrc · 383 weeks and 4 days ago
Chill out man. It's not really about what we're working out. It's about using exising tools in creative ways.
flatcap · 383 weeks and 4 days ago
nice, but somehow unprecise with lower numbers.
oernii3 · 383 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: