Week 1: Homework Solutions

3.

# Calculate the sum of the first n reciprocal squares
n = int(input("Enter number of terms: "))
reciprocal_sum = 0
i = 1
while i <= n:
    reciprocal_sum = reciprocal_sum + i**(-2)
    i = i + 1
print("The answer is", reciprocal_sum)

Euler showed that as n gets larger the limit is π2/6 ≈ 1.6449.

4.

# To calculate all Fibonacci numbers less than 1000
previous, current = 1, 1
print(previous)
while current < 1000:
    print(current)
    previous, current = current, current + previous

To print the ratio of successive terms, you can replace print(current) with print(current/previous). This ratio tends to the golden ratio, $\phi = (\sqrt{5} + 1)/2$.