We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importsystotals=[2,8]cum_totals=[2,10]# finds cumulative total in totals that is lower than ndefcum_total(n):ifn>totals[-1]:whiletotals[-1]<n:totals.append(4*totals[-1]+totals[-2])cum_totals.append(cum_totals[-1]+totals[-1])maxind=len(totals)-1foriinrange(maxind,-1,-1):iftotals[i]<=n:returncum_totals[i]# 4 * last + second_to_lastfibo=[1,2]t=int(input().strip())fora0inrange(t):n=int(input().strip())print(cum_total(n))# Test Cases# 1 0# 2-7 2# 8-33 10# 34-143 44# 144 - : 1# 610# 2584# pattern is that 4 * totals[-1] + totals[-2]
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #2: Even Fibonacci numbers
You are viewing a single comment's thread. Return to all comments →
Faster with optimized algorithm