Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second version even Faster. Can someone be faster? solution in Speedy category for Count Consecutive Summers by Ylliw
count_consecutive_summers=lambda n:sum([n%b*2==b-b%2*b for b in range(1,int((2*n)**.5)+1)])
"""
Is this really working?
Yes it is, it's one of the shortest code to solve the problem and
it's doing it very efficiently as well
2nd version
-----------
How does it work:
sum from p to p+a when p and a are integers is (2p+a)*(a+1)/2
so n=(2p+a)*(a+1)/2
so 2p+a=2n/(a+1)
finally p=(2n-a-a^2)(2*(a+1))
replacing a+1 with b
p=(2n-b(b-1))/2b
and we know that p should b an integer, which means that 2n-b(b-1)%(2b)==0
or 2n%(2b)==b(b-1)%(2b)
Left part can be reduce to n%b*2
Right part can be reduce to (b-1)%2*b which can be written b-b%2*b for 2 characters less
Values for a are from a=0 (sum of a single value) to a=0.5*(-1+sqrt(1+8*n)) when we have the lonest sum of numbers
This value does not make a nice 'golf' code, but 0.5*(-1+sqrt(1+8*n)) is just slightly smaller than sqrt(2n)
for n=1 000 000, this means that the program will evaluate a up to 1414 instead of 1413, acceptable trade off for a shorter code :)
Values for b are therefore from 1 to sqrt(2n)+1
2nd version uses:
- b in place of a+1 for a lot of save characters
- modulo instead of int(x)==x to define if x is integer, greatly save chars but it's much faster (20-30%)
It was a great exerice so found a way to solve it for both code size and speed
"""
April 15, 2019
Comments: