Skip to content

Instantly share code, notes, and snippets.

@christianp
Created January 11, 2012 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianp/1594133 to your computer and use it in GitHub Desktop.
Save christianp/1594133 to your computer and use it in GitHub Desktop.
Find three-digit primes where any reordering of the digits is still prime.
#generate a list of primes, somewhat inefficiently
primes=[2]
for x in range(3,999,2):
if len([y for y in primes if y*y<=x and x % y == 0])==0:
primes.append(x)
#keep only three-digit primes
primes = [x for x in primes if x>100]
#collect primes into anagram classes
k={}
for p in primes:
#convert number to a string then a list, sort it, convert back to string then int
x=int(''.join(sorted(list(str(p)))))
#add this number to the list of numbers anagrammatically equivalent
k[x] = k.get(x,[])+[p]
#now k[x] contains all the prime anagrams of x
#sort the classes of anagrams by length
scores=sorted(k.values(),key=len)
for x in scores:
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment