Results 1 to 13 of 13

Thread: Pi

  1. #1

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Does anyone know of a program that will find PI to any number of digits.

    So the user would just type in 50, and returned you be the 3.14........to fifty digits.

    Thanks


    Rob

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    pi is 4*arctangens(1) and you could use this series to evaluate it:
    pi = sumof[n=0 to infinity]( 4/(2*n+1) - 4/(2*n+2))
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Is there something wrong with my code:

    For n = 0 To 100000
    pii = pii + (4 / (2 * n + 1) - 4 / (2 * n + 2))
    Next
    Debug.Print pii

    I get 2.77257872236476 !!!!!!!

    This is still only 14 digits - no matter how n increases VB will still round it to 14 digits - is there any formula or way to get any amount of digits


    Thanks

    Rob

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I think it's ked's formula that's a bit off, try

    Code:
    For n = 0 To 100000
    pii = pii + (4 / ((4 * n) + 1)) - (4 / ((4 * n) + 3))
    Next
    Debug.Print pii
    VB will always round it's numbers (I can't remember what to tho')

    I'll do you something that'll give you more s.f.
    If it wasn't for this sentence I wouldn't have a signature at all.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    oh yeah, sorry about that. i was thinking there was 2 between each term, but i had 2 terms so there's 4

    well this remembered of an old thread about decimal numbers in strings, so i looked it up:

    http://forums.vb-world.net/showthrea...als#post133046
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Thanks Kedaman - absolutely brilliant code

    Rob

  7. #7

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  8. #8
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Pi

    About that rounding of numbers. I'm not sure how it works in VB.NET these days but in C++ and C# you can solve it like this.

    In C# I do it like this:
    pii = pii + (4.0f / ((4.0f * n) + 1.0f)) - (4.0f / ((4.0f * n) + 3.0f))

    I think back in C++ I did it like
    pii = pii + (4.0 / ((4.0 * n) + 1.0)) - (4.0 / ((4.0 * n) + 3.0))
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  9. #9
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Pi

    To approach this from a totally different angle, you could try the so-called Montecarlo method, based on statistical sampling.
    If you inscribe a circle in a square -so that the sides of the square are tengent to the circle- then if you randomly "throw" a point inside the square, the probability that it lands inside the circle is the ratio of the areas:

    (Pi * r2) / (4 * r2)

    so that by sampling a large number of points the ratio of those that fall in to the total number tends to Pi / 4.

    For example:

    VB Code:
    1. Randomize
    2. 'Initialize counters
    3. Total = 0
    4. In = 0
    5. 'Pick any large number for N
    6. N = 10000
    7. For i = 1 To N
    8.    x = Rnd
    9.    y = Rnd
    10.    Total = Total + 1
    11.    'Compute (squared) distance to origin
    12.    Dist2 = x*x + y*y
    13.    'Check if the point is inside (compare with radius = 1)
    14.    If Dist2 <= 1 Then In = In + 1
    15. Next
    16. Pi = 4 * In / Total
    The shame is this method has a very slow convergence so it's not particularly useful for this calculation, but sometimes the MC method is the only possible or more convenient way to go.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    Addicted Member Rassis's Avatar
    Join Date
    Jun 2004
    Location
    Lisbon
    Posts
    248

    Re: Pi

    I attach an EXCEL file that I use as an introduction to simulation techniques in my classes. It is just a supplement to what Krtxmrtz (seems impossible to pronounce!) said in his last post. It might be interesting especially for those unfamiliar with simulation techniques. It is an experiment attributed to George Buffon (1707 – 1788) http://www-groups.dcs.st-and.ac.uk/~...ns/Buffon.html an ancient French scientist.

    One sheet presents a fixed number of iterations depending on the number of coded cells (2,000 precisely). The other can be run for any number you might want in order to increase statistical significance, but needs additional software for this purpose (I suggest Crystal Ball – there is a link in the attached file) or you program a macro yourself for the repetitions (adapting the code given by Krtxmrtz).

    Enjoy.

    Rui
    Attached Files Attached Files
    ...este projecto dos Deuses que os homens teimam em arruinar...

  11. #11
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Pi

    Quote Originally Posted by Rassis
    ...Krtxmrtz (seems impossible to pronounce!) ...
    What? What? It's easier than some Russian words...!

    Btw, it's a very didactic and nice spreadsheet.
    Last edited by krtxmrtz; Jun 9th, 2006 at 03:32 PM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  12. #12
    Addicted Member Rassis's Avatar
    Join Date
    Jun 2004
    Location
    Lisbon
    Posts
    248

    Re: Pi

    Fiquei contente por ter gostado das minhas aplicações. Uso sempre este design em formação desde há muitos anos e os formandos gostam. É já uma rotina! Gracias.

    Rui
    ...este projecto dos Deuses que os homens teimam em arruinar...

  13. #13
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Pi

    Quote Originally Posted by Rassis
    Fiquei contente por ter gostado das minhas aplicações. Uso sempre este design em formação desde há muitos anos e os formandos gostam. É já uma rotina! Gracias.

    Rui
    Tant de bo que jo hagues tingut la possibilitat d'estudiar matemàtiques d'aquesta manera quan anava a l'escola, en lloc d'haver de llegir llibres avorrits...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width