Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
[4th version] fast (and limited memory) solution in Speedy category for Count Consecutive Summers by Phil15
# I proved on paper that what we want is equal the number of odd divisors of n.
def count_consecutive_summers(n):
while not n % 2: n //= 2 # We first need to get rid of "even" part of n.
# Then apply formula by searching prime numbers less than sqrt(n).
# formula for sigma0 from https://en.wikipedia.org/wiki/Divisor_function#Formulas_at_prime_powers
result, prime = 1, 3
while 1 != n >= prime ** 2:
count = 1
while not n % prime:
n //= prime
count += 1
result *= count
prime += 2 # not always prime but is prime when it divides n.
return result * (1, 2)[n != 1] # if n != 1 then n is prime and has 2 odd divisors.
Dec. 8, 2018