Tuesday, July 30, 2013

Python and Prime number Lists

Prime numbers, although simple in definition, provide several complexities in the field of mathematics, as well as in computer science. For reference, prime numbers are natural numbers which are only divisible by one and themselves. On the surface, this definition seems straightforward and not an important area for research. However, there are a great many applications of prime numbers.  Prime numbers are essentially the building blocks of all other numbers.  For centuries, this unique set of numbers has mesmerized and even baffled some of the greatest mathematicians.  The distribution of prime numbers is another big area of mathematics research. It would be great if we could find a formula for the nth prime. Yet, it seems as though it would be impossible. We can however use computers to do this for us up to the limits of the processing power available.  No real efficient algorithms exist to find primes other than methods of iterating through every number up to the square root of a given number to determine if it is prime.  Finding the patterns with some high level mathematics, is being used but no solid proofs exist yet to revolutionize this area of research.  With this in mind I would like to continue with discussing python in mathematics. 

I previously showed a fairly simple program for Slope-intercept using python. The program served to output a formula in slope-intercept form given two points.   In another post on computer programming in mathematics, I included some Java code for testing if a number is prime. Below I have a Python code to ask for 2 numbers to serve as the starting point and ending point for  listing primes within that range.  I have not included any error checking or input checking,  so it is important to ensure this code works you must enter an integer greater than 1. If you do not do this you will not get a meaningful output or you will get errors.   This was written and tested in Idle 3.2 so it may not work as expected in other versions.  This code also utilizes the standard sieving algorithm to perform its prime checking so it is by no means efficient for larger inputs and can be very processor intensive
.   CAUTION!!!  only use this code to list primes in small ranges CAUTION!!!   The caution is here because I have locked up my computer trying to list primes from 2 – 10,000,000 I would recommend running it on ranges in the 100's It was originally written for educational and research purposes only.   I generally use it to write primes to text files to print out as educational resource materials.

#primecheck(x) checks if x is prime returns boolean#
def primecheck(x):
    for i in range(2,x):
        if x%i == 0:
            return False
    return True
#primelist(start,stop) lists all prime numbers between start and stop#
def primelist():
    start = int(input('Enter starting point must be an integer greater than 1:  '))   
    stop = int(input('Enter ending point must be an integer greater than 1:   '))  
    primeList = []
    for i in range(start,stop):
        if primecheck(i) == True:
    return primeList

print (primelist())

No comments:

Post a Comment