Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
O(1) solution solution in Creative category for Feed Pigeons by martin_b
from math import sqrt
def checkio(n):
# just for fun O(1) solution:
# number of pigeons at any given minute m is m(m+1)/2
# food consumed at any given minute m (if there is enough food) is m(m+1)(m+2)/6
# to get number of minutes required to eat n portions, we have to solve qubic equation m(m+1)(m+2)/6=n, which is trivial thanks to wolframalpha :-)
# then floor(m) gives us number of minutes for which there is enough food for all birds
m=int((sqrt(3)*sqrt(243*n**2-1)+27*n)**(1/3)/3**(2/3) + 1/(3**(1/3)*(sqrt(3)*sqrt(243*n**2-1)+27*n)**(1/3)) - 1)
birds=m*(m+1)//2
f=m*(m+1)*(m+2)//6
# is there some food left for newcommers? if yes, add the fed newcommers
if n-f>birds: birds+=n-f-birds
return birds
Dec. 6, 2015