Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

to calculate the value of π

by TimTim (Initiate)
on Sep 04, 2013 at 13:09 UTC ( [id://1052337]=perlquestion: print w/replies, xml ) Need Help??

TimTim has asked for the wisdom of the Perl Monks concerning the following question:

How to calculate the value of Pi; for the given below infinite series. The output need to be printed after each term is added, stoping after a specified term.. so teh current value need to be printed after each iteration. Pi; = 4-(4/3)+(4/5)-(4/7)+(4/9)-(4/11)....

Replies are listed 'Best First'.
Re: to calculate the value of π
by hdb (Monsignor) on Sep 04, 2013 at 14:14 UTC

    Is it really homework? Then you better NOT submit this:

    use strict; use warnings; use bignum accuracy => 30; use feature 'say'; $/=--$,;say+4*($\+=($/*=-1)/($,+=2))for+1..10;
Re: to calculate the value of π
by MidLifeXis (Monsignor) on Sep 04, 2013 at 13:11 UTC

    This also sounds like homework. Please show what you have tried.

    Also, please note that this should have been posted in SoPW, not meditations. It has been moved.

    --MidLifeXis

      That was also my first reaction, it really looks like homework.

      I remember when I was in college one semester my calculus instructor (who was also the computer science instructor) gave us a scenario in which we had to compute how many steps we needed to perform using the same (Taylor?) series, to generate the value for a particular accuracy. (In one of my computer science classes that same semester {in either Fortran or Pascal-memory fails me now as to which}, she had us implement it.)

      Thanks, OP, for a reminder of some good times. :)

Re: to calculate the value of π
by fisher (Priest) on Sep 04, 2013 at 13:31 UTC
    in addition to previous comment I have to point out that you can't just use a usual division operator (4-4/3 ...) cause then you will found yourself restrained into IEEE 754 boundary (fixed mantissa length).

    you need to implement your own tricky iterative algorithm using pure integer arithmetics. Good luck!

Obligatory comment
by space_monk (Chaplain) on Sep 04, 2013 at 14:20 UTC
    Mmmm pie....
    If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1052337]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-28 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found