an ecommerce website wishes to find the lucky customer who will be eligible for full value cashback. for this purpose, a number n is fed to the system. it will return another number that is calculated by an algorithm. in an algorithm, a sequence is generated, in which each number is the sum of the two preceding numbers. initially, the sequence will have two 1's in it. the system will return the nth number from the generated sequence which is treated as the order id. the lucky customer will be the one who has placed that order. write an algorithm to help the website find the lucky customer.
def lucky_customer(n):
if n <= 2:
return 1
curr_num = 1
prew_num = 1
k = 2
while n > k:
curr_num, prew_num = curr_num + prew_num, curr_num
k += 1
return curr_num
# test
print('order id :', lucky_customer(int(input('n = '))))
Comments
Leave a comment