Results 1 to 10 of 10

Thread: A way to calculate Pi

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Lightbulb A way to calculate Pi

    This is a method that can be used to calculate Pi - the more iterations of i the more accurate the result will be

    Code:
            Dim Counter As Integer = 1
            Dim Answer As Double = 0
            For i = 1 To 100000000
                Answer += (4 / Counter) - (4 / (Counter + 2))
                Counter += 4
            Next
            Debug.Print(Answer)
    Oh btw this uses the Gregory-Leibniz method

    Kris

  2. #2
    Addicted Member
    Join Date
    Jun 2008
    Location
    Macedonia
    Posts
    188

    Re: A way to calculate Pi

    22/7=3,1428571428571428571428571428571
    Pi=3,1415926535897932384626433832795

  3. #3

  4. #4
    Addicted Member
    Join Date
    Jun 2008
    Location
    Macedonia
    Posts
    188

    Re: A way to calculate Pi

    To see that they are not correct

  5. #5
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    1,965

    Re: A way to calculate Pi

    Why wouldn't you just use Math.PI?

  6. #6

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: A way to calculate Pi

    ... because - just showing a way to calculate it

    Kris

  7. #7
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: A way to calculate Pi

    Well that doesn't provide an accurate result at all.

    After the first 2 digits, it's already entirely incorrect.

  8. #8

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: A way to calculate Pi

    Quote Originally Posted by Mathiaslylo View Post
    Well that doesn't provide an accurate result at all.

    After the first 2 digits, it's already entirely incorrect.
    I know this is inaccurate ... just a demo of how to do it using the Gregory-Leibniz method.

    Check this wikipedia link for more details

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: A way to calculate Pi

    Quote Originally Posted by i00 View Post
    I know this is inaccurate ... just a demo of how to do it using the Gregory-Leibniz method.

    Check this wikipedia link for more details
    Reminds me of Professor Frink's sarcasm detector. Comic Book Guy: Now that's really useful ... (machine explodes)

    Just kidding Kris, it's a nice code example. BB

  10. #10
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: A way to calculate Pi

    @i00 Seems like you and I are stuck in the same boat.
    vbnet Code:
    1. Public ReadOnly Property Pi(ByVal Optional limit As Int32 = 25) As Double
    2.         'Pi = (6(1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + ...))^(1/2)
    3.  
    4.         Get
    5.             Dim result As Double
    6.             Dim tmp As Double = 0.0
    7.  
    8.             For i As Int32 = 1 To limit
    9.                 tmp = tmp + (1/i)^2
    10.             Next
    11.             result = (6*tmp)^(1/2)
    12.  
    13.             Return result
    14.         End Get
    15.     End Property

    This is only as accurate as the number of times you process it.. and of course the higher you go the longer it takes. I've pushed it to 100,000,000x before I realized this is not practical for my applications use. 100,000,000x gave me 3.14159264498239. My guess is 1,000,000,000x will produce 3.14159265... so on and so forth.. just as you proposed in your method.
    Last edited by DavesChillaxin; Nov 12th, 2011 at 01:26 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

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