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
  format json outputs ! evilcode1 3 1,692 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 594 Aug-23-2023, 08:52 AM
Last Post: perfringo
  I have written a program that outputs data based on GPS signal kalle 1 1,134 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,342 Jan-04-2022, 07:08 PM
Last Post: ndc85430
Question Problem editing images ielcraft1 2 1,703 Dec-16-2021, 06:48 PM
Last Post: BashBedlam
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,449 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,649 Mar-15-2021, 02:43 PM
Last Post: rybina
  ONE input => THREE outputs Tricia279 6 2,561 Jan-14-2021, 08:52 AM
Last Post: perfringo
  Multi set string inputs/outputs kwmcgreal 2 2,010 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 2,165 Sep-19-2020, 05:46 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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