Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
find a*b=2*number and (a+b)%2=1 solution in Clear category for Count Consecutive Summers by yuwai
def count_consecutive_summers(num):
# if number =m+(m+1)+...+n=(m+n)(n-m+1)/2 ,so (m+n)(let's call it a ) and (n-m+1)(let's call it b )
#are the factors of number*2 ,than m=(a+b-10/2 and n =(a-b+1)
# as m,n,a,b are integers ,so (a+b)%2=1. so the question equals to find a*b=2*number and (a+b)%2=1
count,stop,num_1=1,num,2*num
i=2
while i< stop:
if num_1%i==0 and (i+num_1/i)%2==1:
count+=1
stop=num_1/i
i+=1
return count
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!")
Nov. 6, 2019
Comments: