We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
divs={iforiinrange(2,int(math.sqrt(n))+1)ifn%i==0}divs|={n//i for i in divs}|{n}returnsum(noti&1foriindivs)
Bonus optimizations with bithacks which bring down runtime a lot but still same asymptotic worst case:
twos,n=(n&-n).bit_length()-1,n//(n&-n)divs={iforiinrange(3,int(math.sqrt(n))+1,2)ifn%i==0}divs|={n//i for i in divs}returntwos+twos*(n!=1)+len(divs)*twos
The idea is to first extract the number of 2 factors from N, then find the number of factors of the now-odd N and do some basic combinatorics to come up with the answer.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Divisors
You are viewing a single comment's thread. Return to all comments →
Python golfing, 3 lines, O(sqrt(n))
Bonus optimizations with bithacks which bring down runtime a lot but still same asymptotic worst case:
The idea is to first extract the number of 2 factors from N, then find the number of factors of the now-odd N and do some basic combinatorics to come up with the answer.