Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
pbs solution in Creative category for Saw the Stick by veky
def pbs(cond, start=0):
"""last int after start satisfying cond
Precondition: cond(n)=>cond(n-1) & cond(start) & exists n>start not cond(n)
Complexity: max. 2*ld(result-start), calls cond on distinct numbers only"""
step = 1
while cond(start + step): step <<= 1
start += step >> 1
step >>= 2
while step:
if cond(start + step): start += step
step >>= 1
return start
def checkio(number):
for l in range(pbs(lambda l: 6*number > l**3 - l), 0, -1):
k = round((2*number/l + 1/3 - l**2/12)**.5 - l/2)
if 6*number == 3*k*l*(k + l) + l**3 - l:
return [j*(j + 1)//2 for j in range(k, k+l)]
else: return []
Feb. 10, 2014
Comments: