Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Winters solution in Clear category for Count Consecutive Summers by HeNeArKr
from functools import lru_cache
def count_consecutive_summers(num):
""" Return number of series of consecutive integers whose sum equals 'num',
including the series consisting of just 'num' itself.
"""
@lru_cache(maxsize=512)
def cum_sum(n):
return (n+1) * n / 2
result = 1
# Even number must have at least 3 addends (odd, even, odd), so series
# beginning value cannot be greater than number/3.
end = num // 3 if num % 2 == 0 else num // 2
for i in range(end):
j = 1
value = cum_sum(i+j) - cum_sum(i)
while value < num:
j += 1
value = cum_sum(i+j) - cum_sum(i)
if value == num:
result += 1
return result
Nov. 13, 2019