For those who have heard of the_grandest_staircase_of_them_all problem, this is my simple solution. However, it is not my final solution. My final solution is an optimized version of this. The bigger the value of n, the longer this version will take, since it is recursive and the recursions become deeper and wider when n gets bigger.
def answer(n):
def times(x, y):
count = 1
for k in range(x + 1, y // 2 + 1):
if k < y - k:
count = count + times(k, y - k)
else:
return count
return count
counter = 0
for i in range (1, n):
if i < n-i:
counter = counter + times(i, n-i)
else:
return counter
return counter
def answer(n):
def times(x, y):
count = 1
for k in range(x + 1, y // 2 + 1):
if k < y - k:
count = count + times(k, y - k)
else:
return count
return count
counter = 0
for i in range (1, n):
if i < n-i:
counter = counter + times(i, n-i)
else:
return counter
return counter
No comments:
Post a Comment