Быстрее всего значение вычисляется по формуле Виета
Программа на python:
#(c) NaaMan
import math
def fnc1():
a,b = 1, -1
while True:
c = 1 / a
yield c
a = (abs(a) + 2) * b
b = -b
def fnc2():
a, b = 2, 1
c = a / b
if a > b:
b += 2
else:
a += 2
def fnc3():
a = 1
c = 1 / (a * a)
a += 1
def fnc4():
a = math.sqrt(2)
c = a / 2
a = math.sqrt(2 + a)
leibniz = fnc1( )
vallis = fnc2()
squares = fnc3()
viett = fnc4()
n = int( input( 'n = '))
s1 = 0
s2 = 1
s3 = 0
s4 = 1
while n:
s1 += next( leibniz)
s2 *= next( vallis)
s3 += next( squares)
s4 *= next( viett)
n -= 1
print( 'leibniz = ', s1 * 4)
print( 'vallis = ', s2 * 2)
print( 'squares = ', math.sqrt(s3 * 6))
print( 'viett = ', 2 / s4)
Результаты выполнения программы:
Быстрее всего значение вычисляется по формуле Виета
Программа на python:
#(c) NaaMan
import math
def fnc1():
a,b = 1, -1
while True:
c = 1 / a
yield c
a = (abs(a) + 2) * b
b = -b
def fnc2():
a, b = 2, 1
while True:
c = a / b
yield c
if a > b:
b += 2
else:
a += 2
def fnc3():
a = 1
while True:
c = 1 / (a * a)
yield c
a += 1
def fnc4():
a = math.sqrt(2)
while True:
c = a / 2
yield c
a = math.sqrt(2 + a)
leibniz = fnc1( )
vallis = fnc2()
squares = fnc3()
viett = fnc4()
n = int( input( 'n = '))
s1 = 0
s2 = 1
s3 = 0
s4 = 1
while n:
s1 += next( leibniz)
s2 *= next( vallis)
s3 += next( squares)
s4 *= next( viett)
n -= 1
print( 'leibniz = ', s1 * 4)
print( 'vallis = ', s2 * 2)
print( 'squares = ', math.sqrt(s3 * 6))
print( 'viett = ', 2 / s4)
Результаты выполнения программы: