Python Forum
Help Editing/Problem with outputs - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help Editing/Problem with outputs (/thread-11478.html)



Help Editing/Problem with outputs - stanthaman42 - Jul-10-2018

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:


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



RE: Help Editing/Problem with outputs - ichabod801 - Jul-11-2018

isPrime is counting 0 and 1 as prime. Neither are prime.


RE: Help Editing/Problem with outputs - stanthaman42 - Jul-11-2018

(Jul-11-2018, 01:13 AM)ichabod801 Wrote: isPrime is counting 0 and 1 as prime. Neither are prime.

What Is the best way to fix that without causing issues with other aspects of the data?


RE: Help Editing/Problem with outputs - ichabod801 - Jul-11-2018

You could add:

if i < 2:
    return False
However, note that you are checking the same numbers over and over again to see whether they are prime. I would instead find all the primes up to 10000 and put them in a set. Then you can just check each number to see if it is in the set.

Also, you call find_maxPrimes (which gets the maximum getNumPrimes) and find_maxPrimes2 (which prints all the getNumPrimes), so you are calculating all of the getNumPrimes twice. If you just added the print statement from find_maxPrimes2 to find_maxPrimes, you do it all at the same time.


RE: Help Editing/Problem with outputs - stanthaman42 - Jul-11-2018

(Jul-11-2018, 01:59 PM)ichabod801 Wrote: You could add:

if i < 2:
    return False
However, note that you are checking the same numbers over and over again to see whether they are prime. I would instead find all the primes up to 10000 and put them in a set. Then you can just check each number to see if it is in the set.

Also, you call find_maxPrimes (which gets the maximum getNumPrimes) and find_maxPrimes2 (which prints all the getNumPrimes), so you are calculating all of the getNumPrimes twice. If you just added the print statement from find_maxPrimes2 to find_maxPrimes, you do it all at the same time.
What line would be be best to add the "if i<2:return False"?


Also, I am able to store the number of primes up to 100000 labeled p, but what would be the simplest way to transfer the program i have to check inside of the array p?


RE: Help Editing/Problem with outputs - ichabod801 - Jul-11-2018

The if clause should go after line 35.

If you have the primes in a list p that is a global variable, you can just do:

def isPrime(i):
    return i in p
Note that if p is a set it will be faster than if p is a list. The set can use a hash lookup, while the list has to check each item in turn.


RE: Help Editing/Problem with outputs - stanthaman42 - Jul-11-2018

(Jul-11-2018, 04:37 PM)ichabod801 Wrote: The if clause should go after line 35.

If you have the primes in a list p that is a global variable, you can just do:

def isPrime(i):
    return i in p
Note that if p is a set it will be faster than if p is a list. The set can use a hash lookup, while the list has to check each item in turn.

Thank you very much!

Also, what is the best way to get the outputs into a .txt file or into a excel file?


RE: Help Editing/Problem with outputs - ichabod801 - Jul-11-2018

There is a files tutorial in the tutorials sub-forum, check that out.

If you save each line with the values separated by commas and give it a .csv extension, it will load right into excel when you double click on it.