Skip to content

Instantly share code, notes, and snippets.

@rupalbarman
Created June 8, 2017 10:50
Show Gist options
  • Save rupalbarman/cca3e3d8c209b428499d30a0b6085fac to your computer and use it in GitHub Desktop.
Save rupalbarman/cca3e3d8c209b428499d30a0b6085fac to your computer and use it in GitHub Desktop.
permutations of a number ( digits in it) to print the biggest prime number formed with it
d = []
def isprime(n):
if n<=1: return False
if n<=3: return True
if n%2 == 0 or n%3 == 0: return False
i = 5
while i*i <= n:
if n % i ==0 or n%(i+2) == 0: return False
i+=1
return True
def permutations(s, taken, current):
if bin(taken)[2:].count('1') == len(s):
if isprime(int(current)):
d.append(current)
for i in range(len(s)):
if (taken & (1 << i)): continue
temp = current
temp += s[i]
new_taken = taken | (1 << i)
permutations(s, new_taken, temp)
s = '4691'
permutations(s, 0, '')
print(d, max(d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment