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.
# Enter your code here. Read input from STDIN. Print output to STDOUTfromcollectionsimportOrderedDictcode_p={0:2,1:3,2:5,3:7,4:11,5:13,6:17,7:19,8:23,9:29}N,K=map(int,input().split())n,N=N,N-1ifNin[10**_for_inrange(3,7)]elseNprimes=[0foriinrange(10**len(str(N))-1)]foriinrange(2,len(primes)):forjinrange(i*2,len(primes),i):ifnotprimes[i]==0:continueprimes[j]=1defrootval(n):s=1whilen>0:s*=code_p[n%10]n//=10return(s)primes_d,k_vals=OrderedDict(),OrderedDict()forjin(iforiinrange(2,len(primes))ifprimes[i]==0andi>=1487):ifj<n:k_vals[j]={}val=rootval(j)ifnotvalinprimes_d:primes_d[val]=[j]else:forkinprimes_d[val]:ifkink_vals:forxink_vals[k]:if(j-k)%x==0:k_vals[k][x].append((j-k)//x)ifnotj-kink_vals[k]:k_vals[k][j-k]=[1]primes_d[val].append(j)foriink_vals:iflen(k_vals[i])<K-1:continueforjinsorted(set(k_vals[i])):try:ifk_vals[i][j][K-2]==K-1:print(*[i+(x*j)forxinrange(K)],sep='')exceptIndexErrorase:pass
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #49: Prime permutations
You are viewing a single comment's thread. Return to all comments →
100% python