Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Do the math, O(1) solution in Speedy category for Feed Pigeons by nickie
def checkio(n): # explanation follows...
p = lambda t: t * (t+1) // 2
q = lambda t: (t*t*t + 3*t*t + 2*t) // 6
h = 9*n*n - 1/27
R = 3*n + h**(1/2)
T = 3*n - h**(1/2)
X1 = R**(1/3) + T**(1/3) - 1
w = int(X1)
return p(w) + max(0, n-q(w)-p(w))
"""
p(t): number of of pigeons at round t
p(1) = 1
p(n) = p(n-1) + n
p(n) = 1 + 2 + 3 + ... + n = n*(n+1)/2
q(t): number of portions to feed all pigeons in the first t rounds
q(t)
= \sum_{i=1}^{n} p(i)
= 1/2 * \sum_{i=1}^{n} n^2 + 1/2 * \sum_{i=1}^{n} n
= 1/2 * n * (n+1) * (2*n+1) / 6 + 1/2 * n * (n+1) / 2
= 1/12 * (2*n^3 + 3*n^2 + n) + 1/4 * (n^2 + n)
= 1/12 * (2*n^3 + 3*n^2 + n + 3*n^2 + 3*n)
= 1/12 * (2*n^3 + 6*n^2 + 4*n)
= 1/6 * (n^3 + 3*n^2 + 2*n)
Suppose we start with N portions and w full rounds of pidgeons are fed:
q(w) <= N
<=> w^3 + 3*w^2 + 2*w - 6*N <= 0
Single real root is calculated by:
a = 1, b = 3, c = 2, d = -6*N
f = (3*c/a - b*b/a/a)/3
g = (2*b*b*b/a/a/a - 9*b*c/a/a + 27*d/a)/27
h = g*g/4 + f*f*f/27
R = -(g/2) + h**(1/2)
S = R**(1/3)
T = -(g/2) - h**(1/2)
U = T**(1/3)
X1 = S + U - b/3/a
theferore: w = int(X1)
We can feed p(w) pidgeons and we are left with N - q(w) portions for round w+1.
But the first p(w) pidgeons in round w+1 have already been fed.
So, if N - q(w) > p(w), we can feed N - q(w) - p(w) more pidgeons.
"""
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(1) == 1, "1st example"
assert checkio(2) == 1, "2nd example"
assert checkio(5) == 3, "3rd example"
assert checkio(10) == 6, "4th example"
Dec. 31, 2013
Comments: