loading...

re: Another Way to Find Max Partitions VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Don't listen to this tired programmer who did too much Ocaml lately: the condition is necessary because of how python interprets negative indices. ...

However, once removing the casting to list, summands_iter would be the only version that do not cause an overflow for very large numbers :

def summands_iter(n):
    k = ceil(((1 + 8*n)**0.5 - 1) / 2)
    the_sum = k * (k+1) // 2
    pluck = max(0, the_sum - n)
    return chain(
        range(1, pluck),
        range(pluck+1, k+1))

>>> summands_opt(2**60)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/christophe/python/partition.py", line 17, in summands_opt
    summands = list(range(1, k+1))
MemoryError
>>> for i in summands_iter(2**60): print(i)
# still printing !

Whoa! That was some deep analysis!

Code of Conduct Report abuse