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. 

Sunday, June 30, 2013

Standardized thinking.......

    Despite efforts from Washington, math education is still in trouble in the U.S.  We are now experiencing the so called “math wars”.  Educators are competing with policy makers, and mathematicians are challenging both.  The argument is that educators are focused on the mission and purity of education as a whole.  Education is the basis for lifelong learning and free thought.  When mathematics is concerned there is a wedge between mechanical learning and reasoning.  University mathematicians believe that teaching reasoning before standard algorithms is crippling student‘s abilities to learn the more advanced concepts in college level mathematics.  It is argued that before learning mathematical and numerical reasoning students must understand the standards of math.
The dangers of sticking to a mechanical standard algorithm approach are flexibility, intellectual growth, and individual ability. Flexibility refers to having a standard such as right to left based calculation. This concept alone can prove very confusing especially in younger students who are still honing their reading skills. In most languages, specifically English, students are learning to read from left to right. Every time they are exposed to any form of writing they are taught that it should be understood from left to right. When children begin learning math they are taught to read math in contradiction to all the other reading that they learn. In essence this can cause confusion in both subjects for most students.  Not only is the left to right approach quicker and easier, it also flows logically with the way we are taught to read.
Another issue is that when teachers are grounded with this single algorithm method, students who understand it differently are often punished with bad grades.  Some teachers are so set in the standard right to left and carry method, that if a student chooses to use a different method they are given a bad grade.  The stigma put on outside the norm computation will stunt the students drive and understanding of math. It places in them the “why am I wrong when I got the same answer?” argument.     I had a personal experience with this receiving a failing grade on a test because on the page where you “show your work” I did all multiplication from left to right. The teacher of the class I was in had no clue how that method worked and therefore failed me on my exam “for cheating”. The reason given was that the only way to multiply is the standard right to left so the work shown was an attempt to cover up cheating.  Now this is a specific individual example that does not reflect all teachers, but there are still some that are so set in the standard that “out of box” thinking is “out the window”.
Aside from flexibility, we have the issue of intellectual growth.  Intellectual growth is arguably the central idea of education.  The ability to think and reason is devastatingly uncommon among many adults and children.  Most educators wish to remedy this situation, by trying to step outside of the memorize and regurgitate on the test paradigm.  Standardized testing is standardizing thinking and learning which is destroying innovation and lifelong learning.  While most agree that some concepts should be tested and retained to be successful in college, the ways by which we test and use these concepts need to be modified. 

The last issue is individual ability. Going back to the personal example of being labeled a cheater for utilizing a method that was easiest to me was a stab at my individual abilities. That failure caused me to have to take a much lower level math class then I should have been in twice.  Luckily, the next time around I had a better informed teacher that removed me from that class put me into pre-calculus instead where I aced the exams.  The point is that no matter how you try to standardize people, we are all individuals with individual ability to understand the world around us. 

Saturday, June 8, 2013

The Beal Prize

Andrew Beal a self-made billionaire has announced the prize of one million dollars to the person who can prove the Beal conjecture.  The conjecture is similar to the famous last theorem of Pierre de Fermat.  Fermat’s last theorem, originally written in 1636 remained unsolved until 1995.    The theorem states that no three positive integers a, b, and c can satisfy the equation (an + bn = cn ), for any integer value of n>2.                  The Beal conjecture begins with the equation A X + B Y = C Z and states that if A, B, C, X, Y, and Z are all positive integers > 2 then A, B, and C must share a common factor.  The novice to mathematics might ask the question, what does all of this mean or more importantly why is this conjecture important?  This conjecture along with its predecessor Fermat’s last theorem is closely related to Diophantine equations.  Diophantine equations are a special set of equations named for Greek mathematician Diophantus of Alexandria.
The Diophantine equations all dealt with whole numbers and involved multiple unknown quantities. The most famous of these  equations is the Pythagorean Theorem, which is a2 +b2= c 2 where a and b are the two legs and c is the hypotenuse of a right triangle.  The Pythagorean equation shows the relationship of the lengths of all of the sides.  The real world applications of the Pythagorean Theorem are in engineering, architecture, cartography, and many other areas.  Diophantine equations all find their way into many separate real world applications.   The Beal conjecture is no exception the solution could lead to more applications, than just a simple intellectual curiosity.  Hopefully, this one doesn’t take 3 centuries to solve as did its predecessor.  

The proof of Fermat’s last Theorem was over 100 pages long and took 7 years of isolation for Sir Andrew John Wiles of Britain to complete. Will the Beal conjecture proof take as long? Now that the Fermat proof, which is closely related, is floating around, I propose that the solution to the Beal conjecture is within the Fermat proof or perhaps even an expansion or generalization of that proof. 

Monday, May 13, 2013

Better learning through abstraction..…


There are many barriers to learning and teaching mathematics, some of which include:  anxiety, comprehension, and focus.  How do we get past these?  First, let’s look at anxiety. Why does the mere mention of mathematics strike fear and confusion in students?   It seems there are so many different causes of this to look at, that we are unable to pin point a single method to fix the problem.  In fact to find the solution, we need use the solution.   You will say   “well that sounds impossible”.   If we don’t know the solution, how can we use the solution to find it? There is a simple answer, abstraction.
I touched upon the idea of abstraction in Algebra: the true heart of mathematics.  So rather than just focusing on a solution to all of the different barriers to learning mathematics, we instead use abstraction to find a general solution.  We can then begin teaching abstraction rather than just mathematics.   If we find ways to take the classic “math” out of the picture we avoid the instant fear that many students face.  The statement “Today, class we are going to invent a new dictionary” is much less daunting than “Today, class we are going to learn algebra”.   The first statement will immediately spark a curiosity. The students will begin asking questions, and wanting to know more. 
What does inventing a new dictionary have to do with algebra?  To answer this, we first need to think what is algebra?  Algebra is basically a “dictionary” of formulas, theorems, and proofs which apply to a wide range of problems. 
How do these relate to each other?  Let’s look at the commutative property of addition. In algebra this is “defined” as   (a + b = b + a) .    To look at this in words rather than algebraic symbols, you get 

 Commutative property of addition: When adding two numbers together the order of the numbers does not change the result of the addition.


Where do we go from here?  This seems no different than how algebra is introduced normally.  What I am proposing is taking the math out of it in the beginning, and then slowly adding the math in after the concept of abstraction is understood.  We ask the students to look at some common problems they may face in their daily lives.  We will then list out some of the top suggestions. The students will then be asked to find the commonalities between the problems to group them together. Then the students are asked to make a name for each of the problem groups.  They can then begin defining the general solution to those problem types. Keep it simple, only spend about a week introducing the concept of abstraction with simple problems.   After the idea of abstraction seems clear then you could start adding the Math back into the process. You will begin with asking the students about math related problems. You could start with something simple like what is an even number. Many of the students by this point are familiar with the idea of what an even number is.  However, this time you can approach the same way you approached your dictionary idea. You ask the students to create their own definition of an even number. Allowing them to create their own mathematical rules to use empowers them with the ability to not only recognize the steps of problem solving, but to understand it rather than just memorize it.

               Teaching abstraction leaves a wide open doorway to many different possibilities. The hardest part of learning math for many students is the daunting amount of formulas and rules to remember. A student who knows how and why these rules work, and can look at a problem and devise their own algorithms for solving it, will be way ahead of the curve when they are faced with solving any problem they encounter. When you think about it you use this concept every day. As adults we face many different types of problems, and we solve them by first identifying the problem then abstracting a set of procedures for solving them. We sometimes have similar problems that we have solved before, and we encounter new problems that need new solutions.  However, being successful in life is dependent on the solidarity and efficiency of our ability to solve problems. When I say problems I don’t mean just mathematical, it is the seemingly mundane parts of life for some and the more perplexing or dire situations we encounter that I call problems.         Abstraction, logic, deductive and inductive reasoning are so important to a successful life that part of the curriculum of each grade level should be dedicated to them.

Saturday, February 2, 2013

Analysis of the MegaMIllions lottery



I compiled some data in Excel using the winning numbers data from 5-17-2002 – 3-23-2012.A few of the charts created using the winning numbers for the Mega Millions lottery are found below.
This first chart describes the number of times each number is drawn for each pick. From the data we see that the number 2 is more likely to be the first number drawn because it shows up as the first pick more often. The number of times 2 has appeared first is 75.4% of the total number of times a 2 has been in the winning numbers.


How many times each numbers is drawn 




first pick
second pick
third pick
fourth pick
fifth pick
mega ball
1s
82
0
0
0
0
19
2's
92
9
0
0
0
21
3s
78
7
1
0
0
27
4's
70
23
1
0
0
24
5's
68
22
3
1
0
18
6's
61
21
3
0
0
22
7's
63
24
6
1
0
28
8's
53
27
4
0
0
22
9's
52
36
6
0
0
27
10's
50
46
13
0
0
27
11's
39
32
10
3
0
17
12's
35
44
16
2
0
21
13's
27
49
15
2
0
27
14's
46
40
19
2
1
16
15's
24
39
19
4
0
23
16's
32
42
16
10
0
15
17's
17
50
30
3
0
17
18's
23
40
27
10
1
21
19's
12
38
30
11
0
22
20's
13
40
38
7
1
24
21's
10
33
35
13
2
30
22's
14
34
32
13
4
25
23's
9
27
30
12
2
18
24's
14
32
36
10
3
23
25's
10
33
29
19
4
20
26's
5
25
39
19
2
22
27's
9
25
42
16
7
20
28's
5
20
35
34
5
9
29's
4
26
33
30
7
25
30's
2
23
27
29
8
21
31's
2
25
40
29
12
21
32's
1
19
34
35
10
15
33's
1
11
25
29
13
23
34's
0
8
31
29
11
26
35's
1
12
30
29
16
28
36's
1
10
25
41
21
32
37's
1
5
33
37
7
17
38's
1
6
25
40
22
25
39's
1
7
30
46
23
18
40's
0
5
19
45
23
20
41's
0
3
20
28
26
19
42's
0
3
18
43
29
23
43's
0
4
22
27
31
20
44's
0
1
18
32
42
21
45's
0
1
19
38
37
19
46's
0
1
11
44
47
17
47's
0
0
7
35
40
7
48's
0
0
12
41
55
4
49's
0
0
3
33
54
7
50's
0
0
2
25
74
4
51's
0
0
4
26
81
1
52's
0
0
2
17
89
10
53's
0
0
2
16
56
0
54's
0
0
1
8
50
0
55's
0
0
0
4
49
0
56's
0
0
0
0
63
0


In this next chart I have laid out the average, mode, median, harmonic mean and other analysis of the number distributions across all of the different picks. 



first pick
second pick
third pick
fourth pick
fifth pick
mega ball
median all
7.5
18
28
38
49
24
Average all
9.278210117
18.6624514
28.3015564
37.42607
46.4542802
23.990272
mode all
2
17
27
39
52
36
standard deviation
7.137233978
9.10326996
10.2470703
9.59011029
7.6711884
13.842004
Geometric mean
6.565968069
16.139646
26.1471469
35.9559721
45.6978254
18.277583
Harmonic mean
4.207752217
13.1495089
23.5028125
34.0860285
44.7726832
10.698143
Mean - stddev
2.140976139
9.5591814
18.0544862
27.8359598
38.7830918
10.148268
Mean + stddev
14.63723398
27.10327
38.2470703
47.5901103
56.6711884
37.842004
avg mean+-stddev
8.389105058
18.3312257
28.1507782
37.713035
47.7271401
23.995136


This final chart is all of the data above rounded to the nearest whole number.
first pick
second pick
third pick
fourth pick
fifth pick
mega ball
8
18
28
38
49
24
9
19
28
37
46
24
2
17
27
39
52
36
7
9
10
10
8
14
7
16
26
36
46
18
4
13
24
34
45
11
2
10
18
28
39
10
15
27
38
48
57
38
8
18
28
38
48
24

It is important to note, that all these numbers represent the various distribution statistics of the numbers that were drawn at each of the televised drawings, and do not represent any regressive or other types of predictive models. These numbers are just simply a statistical analysis of the given data for educational purposes only. All data represents the real numbers published at http://www.megamillions.com/numbers/pastdrawings.asp The website offers a tool at the top of the page to select your data range and get all of the drawings