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):
    i=2
    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:   '))  
    i=2
    primeList = []
    for i in range(start,stop):
        if primecheck(i) == True:
            primeList.append(i)
    return primeList


print (primelist())

Sunday, July 28, 2013

Python Slope-Intercept

Python  is a wonderful programming language for quickly coding solutions to many problems. You can download python by going to python.org/download and selecting the version that’s best for your operating system.  Python can run from command line or though Idle that comes with most python downloads.   I have been working with python for almost a year now and it has become my “go to “for  
a lot of work in mathematics. It is not necessarily the “best” language for processor intensive mathematical algorithms, but it can offer efficient and quick solutions that can be easily applied to larger projects.  Recently, I have been doing some review of linear algebra and slope-intercept formulas. This is a fairly simple subject in mathematics but I thought it might be interesting to use python to solve some of these problems.   My solution might not be elegant but it is fairly simple.  I designed the program to take in two points (x1, y1), (x2, y2)  and output an equation of the line in slope-intercept form.   For those who may not be familiar slope-intercept form it is  y = mx + b where m is the slope and b is the y- intercept. The slope is calculated from the two given points by the formula (y1-y2)/(x1-x2) and the y -intercept can be found by 
b = y – mx.


Slope-intercept is useful in linear depreciation models.  For example:  if you bought a car at $81,292 and after 10 years it is only worth $33,121 to find out how much it will be worth after 13 years you can use slope-intercept to get a depreciation formula. The two points are at 0 years it is 81,292 (0, 81,292) and at 10 years it is 33,121 (10 , 33,121) substituting these numbers into our formulas you first find the slope which will represent the rate of depreciation.  You use the slope formula (y1-y2)/(x1-x2) 
(33,121 – 81,292) / (10-0) = -48,171 / 10 = -4817.1   and the y intercept is b = y – mx. 81292- 0(4817.1) = b= 81292 and putting it together you get y = (-4817.1) x + 81292 using this formula we can then determine the value of the car at 13 years by substituting 13 in for x
y = (-4817.1)13 + 81292 and we get 18,669.70 so after 13 years our car is worth $18,669.70
The Python code to find our linear depreciation formula is fairly straight forward


def slope():

    result = ''
    x = float(input("enter x one"))
    y = float(input("enter y one"))
    a = float(input("enter x two"))
    b = float(input("enter y two"))
    slope = (y-b)/(x-a)
    yint = y - (slope*x)
    if yint < 0:
        sign='-'
    else:
        sign='+'
    result = str("y="),str(slope),str("x"),sign,str(yint)
    return result
print(slope())


I use the input() method to take in the two points (x,y), (a,b)
then create a variable named “slope” and then immediately assign it to the formula to find the slope given 2 points (y-b)/(x-a). I then create a variable yint and assign it to the formula for the y-intercept. The if-else statement I use to aid with formatting because generally the + (plus sign) isn't displayed when you get a positive. The final line print(slope()) is there only for when you are running the code in console or idle if you were to convert this to a python module it won't be needed.  
I am sure there are more elegant and efficient ways of doing this as well as built-in python methods to make it display much better but that’s the beauty of python it’s easy to modify and play around with to get desired output style.