Friday, May 13, 2016

Python itertools efficient looping tools

I created a python program to solve the Goldbach conjecture.  This is nothing more than get a number from the user, checks to make sure that it is even, and then finds two prime numbers that sum to the number.  To my surprise it was a bit tricky and I had to rewrite my program a few times before it started to make sense.

One tool that was extremely powerful was the itertools  I was able to use the itertools.permutations function that will perform the following arrangement for me to map out all possible cases.
Here is an example:
list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2), (3, 4),
(4, 1), (4, 2), (4, 3)]
Credit To: how to generate all permutations of a list in python & Python Itertools
I listed my programs below if anyone was looking for an example.
Here is my chap8_5_prog_exercise.py:
import sys
import math
import time


def calcPrime(number):
    nSqrt = math.sqrt(number)
    n = number / nSqrt
    
    return n, number%n==0


def main():
    num = eval(input("Enter a number:"))
    n = 0
    nSqrt = 0
    if num < 2:
        sys.exit(0)
    
    n, status = calcPrime(num)
    print(status)
    
    if status == True:
        print("Number:",n, "is prime")
    else:
        while status != True:
            n, status = calcPrime(num)
            print(n, "and", status, "Index", num,)
            num += 1
            time.sleep(.1)
        print("Number:",num-1, "is prime")

if __name__ == "__main__":
    main()

Here is my chap8_6_prog_exercise.py:
import sys
import time
import itertools
from chap8_5_prog_exercise import calcPrime

def modcheckSumOfTwoNumbers(bList,inputNum, index):
     
    try:
        for x in bList:
            #print("X", x, "x[0]+x[1]", x[0]+x[1], "inputNum", inputNum)
            if int(x[0])+int(x[1])==inputNum:
                return x, True
            index += 1
    except IndexError:
        gotdata = 'null'
    
    return [0,0], False
        
    

def main():
    inputnum = eval(input("Enter a number:"))
    num = inputnum
    status = False
    SumOfTwoNumbersStatus = False
    aList = list()
    bList = list(tuple())
    cList = list()
    
    if inputnum%2 != 0 :
        print("Number is not even")
        sys.exit(0)
    
    aList.append(1)
    
    if inputnum == 2:
        cList.append(1)
        cList.append(1)
    else:
        indexInner = 0
        while not SumOfTwoNumbersStatus:
            num += 1
            n, status = calcPrime(num)
            if status == True:
                bList = list(itertools.permutations(aList, 2))
                aList.append(n)
                cList, SumOfTwoNumbersStatus = (modcheckSumOfTwoNumbers(bList,inputnum, 0))
                indexInner += 1
            time.sleep(.25)
    
    print("The sum of the two primes that equal the number submitted: "+"{0:0}".format(inputnum)+" are: "+"{0:0}".format(int(cList[0]))+"+"+"{0:0}".format(int(cList[1])))

if __name__ == "__main__":
    main()

No comments:

Post a Comment