A way to calculate Pi-VBForums

# Thread: A way to calculate Pi

1. ## 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
Oh btw this uses the Gregory-Leibniz method

Kris

2. ## Re: A way to calculate Pi

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

3. ## Re: A way to calculate Pi

Why do you show like 40 digits if only the first two are correct lol.

4. ## Re: A way to calculate Pi

To see that they are not correct

5. ## Re: A way to calculate Pi

Why wouldn't you just use Math.PI?

6. ## Re: A way to calculate Pi

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

Kris

7. ## 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. ## Re: A way to calculate Pi

Originally Posted by Mathiaslylo
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. ## Re: A way to calculate Pi

Originally Posted by i00
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. ## Re: A way to calculate Pi

@i00 Seems like you and I are stuck in the same boat.
vbnet Code:
`Public ReadOnly Property Pi(ByVal Optional limit As Int32 = 25) As Double        'Pi = (6(1/1^2 + 1/2^2 + 1/3^2 + 1/4^2 + ...))^(1/2)         Get            Dim result As Double            Dim tmp As Double = 0.0             For i As Int32 = 1 To limit                tmp = tmp + (1/i)^2            Next            result = (6*tmp)^(1/2)             Return result        End Get    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.

#### Posting Permissions

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

Featured