However, once removing the casting to list, summands_iter would be the only version that do not cause an overflow for very large numbers :
defsummands_iter(n):k=ceil(((1+8*n)**0.5-1)/2)the_sum=k*(k+1)//2pluck=max(0,the_sum-n)returnchain(range(1,pluck),range(pluck+1,k+1))>>>summands_opt(2**60)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"/home/christophe/python/partition.py",line17,insummands_optsummands=list(range(1,k+1))MemoryError>>>foriinsummands_iter(2**60):print(i)# still printing !
However, once removing the casting to list,
summands_iter
would be the only version that do not cause an overflow for very large numbers :Whoa! That was some deep analysis!