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.
This gives me a timeout error on 5 cases. Can anyone help?
frommathimportgcdfromitertoolsimportpermutationsdeftotatives(n):phi=int(n>1andn)forpinrange(2,int(n**.5)+1):ifnotn%p:phi-=phi// pwhilenotn%p:n//= p#if n is > 1 it means it is primeifn>1:phi-=phi// n returnphidefpermute(num,phi_num):temp="".join(sorted(str(num)))phi_num="".join(sorted(str(phi_num)))returntemp==phi_numN=int(input())d={}forninrange(12,N):ifpermute(n,totatives(n)):#print(permute,phi(n))d[n]=(n/totatives(n))#print(d)min_b=min(d.values())fora,bind.items():ifb==min_b:print(a)break
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #70: Totient permutation
You are viewing a single comment's thread. Return to all comments →
This gives me a timeout error on 5 cases. Can anyone help?