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.
fromfractionsimportgcdgcache={}hcache={}defG(n):ifningcache:returngcache[n]sqrtN=int(n**.5)s=n*(n+1)// 2forginrange(2,sqrtN+1):s-=G(n// g)ifg!=n// g:s-=G(g)*(n// g - n // (g + 1))ifn>1:s-=G(1)*(n// 1 - n // (1 + 1))gcache[n]=sreturngcache[n]defH(n):ifninhcache:returnhcache[n]s=nsqrtN=int(n**.5)forginrange(2,sqrtN+1):s+=n// g - H(n // g // g)hcache[n]=sreturnsdefS(n):s=0sqrtN=int(n**.5)s-=G(sqrtN)s+=H(n)returns-n+1n=int(input())print(S(n))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Coprime Conundrum
You are viewing a single comment's thread. Return to all comments →
Python3 solution