Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Count Consecutive Summers by Kurush
# I use the arithmetic progression formula
# to determine the first element and the count of elements
# S_n = [a_1 + (n - 1) / 2] * n
def count_consecutive_summers(num):
summers = []
n = 1
addendum = n * (n - 1) // 2
while (num >= addendum):
a1 = (num - addendum) / n
if a1.is_integer() and a1 > 0: summers.append((a1, n))
n += 1
addendum = n * (n - 1) // 2
return len(summers)
if __name__ == '__main__':
print("Example:")
print(count_consecutive_summers(42))
# These "asserts" are used for self-checking and not for an auto-testing
assert count_consecutive_summers(42) == 4
assert count_consecutive_summers(99) == 6
assert count_consecutive_summers(1) == 1
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 20, 2019