123glkj
123glkj

Reputation: 3

Calculating the value of pi using a while loop in Python v3

I am new to this platform and not sure how to write it as code. And I’ve tried doing this question different ways but my value of pi is 3.414 so I am not sure where I am going wrong.

image of the formula I am to use

from math import sqrt

root = sqrt(2)
denominator = sqrt(2 + root)
while 2 * (2 / sqrt(2 + denominator) > 1):
    denominator *= denominator
pi = denominator
print("Approximation of pi: %s" % (round(pi, 3)))

Upvotes: 0

Views: 2219

Answers (1)

vaz
vaz

Reputation: 330

What about something like (looks more clear to me):

from math import sqrt

root = 2*(2/sqrt(2))
denominator = sqrt(2)
pi = root
while 2 / sqrt(2 + denominator) > 1:
    pi = pi * 2 / sqrt(2 + denominator)
    denominator = sqrt(2 + denominator)
print("Approximation of pi: %s" % (round(pi, 3)))

Upvotes: 1

Related Questions