Python Forum
Help Editing/Problem with outputs
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Editing/Problem with outputs
#1
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
Reply
#2
isPrime is counting 0 and 1 as prime. Neither are prime.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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?
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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?
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multi set string inputs/outputs kwmcgreal 2 253 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  Editing a txt document using python duckredbeard 3 314 Sep-24-2020, 02:38 AM
Last Post: duckredbeard
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 325 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  I Need Help Editing My Code eddywinch82 10 1,038 Aug-29-2020, 03:54 PM
Last Post: eddywinch82
  Outputs missing SamAnw 4 464 Feb-12-2020, 04:32 PM
Last Post: adetheheat
  Code Editing Help Rememberthecreed 2 580 Feb-08-2020, 09:56 AM
Last Post: Rememberthecreed
  Indexed position editing of a lst ShruthiLS 4 540 Sep-26-2019, 09:23 AM
Last Post: perfringo
  Interpreter and running a .py file give different outputs PythonNPC 5 750 Jul-21-2019, 01:07 PM
Last Post: PythonNPC
  Editing and testing a package amasta 2 795 Mar-19-2019, 12:36 PM
Last Post: buran
  Save outputs to Desktop cisicomania 1 967 Aug-31-2018, 10:08 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020