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.
It seems to be OK to post code so let me share mine to show that you don't need lots of code to do it in Python
...frommathimportisqrtfromitertoolsimportaccumulateNMAX=10**12TMAX=math.isqrt(2*NMAX)+2TOT=NoneACC=Nonedeftotient_init(n):globalTOTTOT=list(range(n+1))foriinrange(2,n+1,2):TOT[i]//= 2p=3whilep<=n:ifTOT[p]==p:#nottouchedyet,thatis:primeforiinrange(p,n+1,p):TOT[i]=TOT[i]*(p-1)// pp+=2defsolve(l,r):# prepare totient table etcglobalTOT,ACCifnotTOT:totient_init(TMAX)# for even i we need phi(i/2) not phi(i), different for i=4*kforiinrange(4,TMAX+1,4):TOT[i]//= 2# multiply neighbourstot_it=iter(TOT)next(tot_it)ACC=[a*bfora,binzip(TOT,tot_it)]ACC=list(accumulate(ACC))returnACC[tri_down(r)]-ACC[tri_up(l)-1]deftri_up(n):#idxofnexttriangularnumberatnorabovei=isqrt(2*n)ifi*(i+1)//2 < n: i+=1returnideftri_down(n):#idxofnexttriangularnumberatnorbelowi=isqrt(2*n)ifi*(i+1)//2 > n: i-=1returni
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A Weird Function
You are viewing a single comment's thread. Return to all comments →
It seems to be OK to post code so let me share mine to show that you don't need lots of code to do it in Python