uaveiro-leci/1ano/fp/aula04/leibnizPi4.py

22 lines
386 B
Python
Raw Normal View History

2022-10-14 22:03:25 +00:00
import math
2023-01-30 16:48:43 +00:00
2022-10-14 22:03:25 +00:00
def leibnizPi4(n):
total = 0
for x in range(1, n+1):
2023-01-30 16:48:43 +00:00
increment = 1/(x*2-1)
total += -increment if x % 2 == 0 else increment
2022-10-14 22:03:25 +00:00
return total
2023-01-30 16:48:43 +00:00
2022-10-14 22:03:25 +00:00
def main():
num = int(input('Introduza o número de termos: '))
2022-10-18 17:18:56 +00:00
print(f"""
Resultado da série de Leibniz: {leibnizPi4(num)}
Valor do PI/4: {math.pi/4}
""")
2022-10-14 22:03:25 +00:00
2023-01-30 16:48:43 +00:00
2022-10-18 17:18:56 +00:00
if __name__ == "__main__":
2023-01-30 16:48:43 +00:00
main()