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 STDOUTimportmathfromitertoolsimportpermutationsaspprimes_p,pan_d=[],list('123456789')defis_prime(n):foriinrange(2,1+int(math.sqrt(n))):ifn%i==0:return(False)else:return(True)foriinrange(3,10):# an prime ends with only 9,7,3 or 1 prim_e=''.join(_for_in'9731'ifint(_)<=i)forjinprim_e:s=pan_d[:i]s.remove(j)#iterating through the permutations of numbers without 9 or 7 or 3 or 1 as it is the ending digit, this reduces the solution set to potential prime numbersforkinp(s):n=int(''.join(k)+j)ifis_prime(n):primes_p.append(n)#print(primes_p)primes_p.sort()defsearch(v,l,r,a):ifl==r:ifl==0:return(-1ifv<a[l]elsea[l])elifv<a[l]:return(a[l-1])else:return(a[l])m=(l+r)//2ifv<=a[m]:return(search(v,l,m,a))else:return(search(v,m+1,r,a))fora0inrange(int(input())):print(search(int(input()),0,len(primes_p)-1,primes_p))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #41: Pandigital prime
You are viewing a single comment's thread. Return to all comments →
python 100%