Hello there. I am Currently working on a program that produces every number (1000-9999) and their prime permutations(single, double, triple, and quadruple digit numbers). When I print my output, it is telling me that 1000 has 2 prime permutations which is incorrect. Can anyone see if I made a mistake with my coding that might be easy to fix or what could be the cause of the wrong data?
Thanks, the code is posted below:
Thanks, the code is posted below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import math def getAllCombinations(s, length): if length = = 0 : return [''] else : ret = [] for idx, c in enumerate (s): combos = getAllCombinations(s[ 0 :idx] + s[idx + 1 :], length - 1 ) for i in range ( len (combos)): combos[i] = c + combos[i] ret + = combos return ret def getPermutations(s, ret, swapIdx = 0 ): if swapIdx = = len (s): ret.append( int (''.join(s))) for i in range (swapIdx, len (s)): cpy = [c for c in s] cpy[swapIdx], cpy[i] = cpy[i], cpy[swapIdx] getPermutations(cpy, ret, swapIdx + 1 ) def getAllPermutations(i): s = str (i) allPerms = set () for i in range ( len (s)): curCombos = getAllCombinations(s, i + 1 ) for combo in curCombos: ret = [] getPermutations(combo, ret) allPerms = allPerms.union( set (ret)) return list (allPerms) def isPrime(n): for i in range ( 2 , math.ceil(n * * ( 1 / 2 )) + 1 ): if n % i = = 0 : return False return True def getNumPrimes(i): perms = getAllPermutations(i) numprimes = 0 for perm in perms: if (isPrime(perm)): numprimes + = 1 return numprimes def find_maxPrimes(): max = 0 maxNum = 0 for i in range ( 1000 , 10000 ): cur = getNumPrimes(i) if ( max < cur): max = cur maxNum = i return maxNum print (find_maxPrimes()) def find_maxPrimes2(): # max = 0 # maxNum = 0 #Printing here directly for i in range ( 1000 , 10000 ): print (i, " ---> " , getNumPrimes(i)) find_maxPrimes2() |
Output:1000 ---> 2
1001 ---> 4
1002 ---> 2
1003 ---> 7
1004 ---> 5
1005 ---> 3
1006 ---> 4
1007 ---> 8
1008 ---> 2
1009 ---> 6
1010 ---> 4