# define this function using loop:
def right_year_loop(n):
"""
to get the correct year that is the sum between 1 to 1024
"""
i=0
j=1
correct_year=0
while i<n:
correct_year+=j
i+=1
j=j*2
return correct_year
# define this function using recursion:
def right_year_recursion(n):
"""
to get the correct year that is the sum between 1 to 1024, by recursion
"""
if n==0:
correct_year=1
correct_year=right_year(n-1)+2**n
return correct_year
import time
#define a function that compares the efficiency
def eff_time(n):
t0=time.Clock()
right_year_loop(n)
t1=time.Clock()
right_year_recursion(n)
t2=time.Clock()
if t2-t1 < t1-t0:
print("recursion is faster than pure loop")
else:
print("recursion is slower than pure loop")
# execute the comparison
eff_time(11)
# result
2047
2047
"recursion is faster than pure loop"