def factorial():
f, n = 1, 1
while True: # First iteration:
yield f # yield 1 to start with and then
f, n = f * n, n+1 # f will now be 1, and n will be 2, ...
for index, factorial_number in zip(range(51), factorial()):
print('{i:3}!= {f:65}'.format(i=index, f=factorial_number))
def fib():
a, b = 0, 1
while True: # First iteration:
yield a # yield 0 to start with and then
a, b = b, a + b # a will now be 1, and b will also be 1, (0 + 1)
for index, fibonacci_number in zip(range(100), fib()):
print('{i:3}: {f:3}'.format(i=index, f=fibonacci_number))
from decimal import Decimal, getcontext
getcontext().prec=60
summation = 0
for k in range(50):
summation = summation + 1/Decimal(16)**k * (
Decimal(4)/(8*k+1)
- Decimal(2)/(8*k+4)
- Decimal(1)/(8*k+5)
- Decimal(1)/(8*k+6)
)
print(summation)