Activity - Python 101#
Exercise 1#
An approximation of \(\pi\):
In the 17th and 18th centuries, James Gregory and Gottfried Leibniz discovered an infinite series that can be used to calculate \(\pi\):
Implement a function called pi_approx to estimate the value of \(\pi\) using the Leibniz method, where the argument must be an integer \(n\) indicating the number of terms added, that is:
def pi_approx(n):
"""
Approximation of $\pi$ using Leibniz's method.
Parameters
----------
n : int
Number of terms added.
Returns
-------
output : float
"""
pi = #FIXME# # Initialization
for k in range(#FIXME#):
numerator = #FIXME#
denominator = #FIXME#
pi += # Add numerator/denominator to total sum
return 4 * pi
Cell In[1], line 15
pi = #FIXME# # Initialization
^
SyntaxError: invalid syntax
pi_approx(100)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 1
----> 1 pi_approx(100)
NameError: name 'pi_approx' is not defined
Check your answer! pi_approx(100) must return 3.1315929035585537
Exercise 2#
Let \(\sigma(n)\) be defined as the sum of the proper divisors of \(n\) (that is, less than \(n\)). Amicable numbers are positive integers \(n_1\) and \(n_2\) such that the sum of the proper divisors of one is equal to the other number and vice versa, that is :
For example, the numbers 220 and 284 are friendly numbers.
Proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110, hence \(\sigma(220) = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284\).
Proper divisors of 284 are 1, 2, 4, 71 and 142, hence \(\sigma(284) = 1 + 2 + 4 + 71 + 142 = 220\).
Implement the following functions:
proper_divisors(n)such that returns alistof the proper divisors of \(n\).sigma(n)such that it returns the value \(\sigma(n)\).friends(n, m)such that it returnsTrueif \(n\) and \(m\) are friendly numbers andFalseotherwise.
def proper_divisors(n):
proper_divisors = []
for i in range(#FIXME#)
if #FIXME#: # Hint: Use modulus operation
proper_divisors.#FIXME#
return #FIXME#
Cell In[3], line 3
for i in range(#FIXME#)
^
SyntaxError: '(' was never closed
proper_divisors(220)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 proper_divisors(220)
NameError: name 'proper_divisors' is not defined
def sigma(n):
proper_divisors_list = #FIXME#
sum = 0
for x in proper_divisors:
sum#FIXME#
return sum
Cell In[5], line 2
proper_divisors_list = #FIXME#
^
SyntaxError: invalid syntax
sigma(220)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 1
----> 1 sigma(220)
NameError: name 'sigma' is not defined
def amicables(n, m):
sigma_n = #FIXME#
sigma_m = #FIXME#
if #FIXME#:
return True
else:
return #FIXME#
Cell In[7], line 2
sigma_n = #FIXME#
^
SyntaxError: invalid syntax
n, m = 220, 284
print(f"Proper divisors of {n} are {proper_divisors(n)}, then sigma({n}) = {sigma(n)}")
print(f"Proper divisors of {m} are {proper_divisors(m)}, then sigma({m}) = {sigma(m)}")
print(f"Are 220 and 284 amicable numbers?: {amicables(n, m)}")
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 2
1 n, m = 220, 284
----> 2 print(f"Proper divisors of {n} are {proper_divisors(n)}, then sigma({n}) = {sigma(n)}")
3 print(f"Proper divisors of {m} are {proper_divisors(m)}, then sigma({m}) = {sigma(m)}")
4 print(f"Are 220 and 284 amicable numbers?: {amicables(n, m)}")
NameError: name 'proper_divisors' is not defined