hw5_OddEvenPrimeSqua..

advertisement
HW5 – Functions
Homework Requirements


Use emacs to create a hw5.py file in your hw5 directory.
This file should have a complete file header comment, function header comments, and should follow all
of the other 201 Python coding standards.
Number Theory
The study of mathematics is separated into many fields. The one that tends to lead students to discover the
beauty of mathematics is the field of number theory. This field deals with numbers and their relationships to
each other.
The field of number theory has lead to discoveries such as:
 Prime numbers -- those which can only be divided by themselves and one.
Some special primes that you might find interesting are of the form 2p - 1, where p is prime. These are
called Mersenne Primes after the French monk Marin Mersenne who discovered them. The search for
Mersenne primes continues. You can participate in the search for new Mersenne primes by joining
GIMPS (the Great Internet Mersenne Prime Search). The 47th known Mersenne prime, 242,643,801 - 1, a
12,837,064 digit was found on April 12, 2009 by Odd Magnar Strindmo from Melhus, Norway. The
45th and largest known Mersenne prime is 243,112,609 - 1, a 12,978,189 digit number, was discovered on
August 23, 2008 by Edson Smith at UCLA. On September 6, 2008 the 46th known Mersenne prime,
237,156,667 - 1, an 11,185,272 digit number was found by Hans-Michael Elvenich in Langenfeld,
Germany. This was the first Mersenne prime to be discovered out of order since 2110,503 - 1 in 1988.
 Perfect numbers -- ancient mathematicians were interested in the relationship of a number and the sum
of its divisors. A positive integer that is equal to the sum of its divisors (excluding itself) is called a
"perfect" number. For example, the divisors of 6 are 1, 2 and 3. Since 6 = 1 + 2 + 3, 6 is a perfect
number. The next three perfect numbers are 28, 496 and 8128. If the sum of the divisors is greater than
the number, the number is called "abundant". If the sum of the divisors is less than the number, the
number is called "deficient". Perfect numbers demonstrate some interesting properties -- all known
perfect numbers end in 6 or 8; there are no known odd perfect numbers.
 Squares -- many positive integers are considered geometric in nature. You learned about perfect squares
in high school -- 1, 4, 9, 16, etc. They are called "squares" because a group of 1, 4, 9 or 16 dots can be
arranged in a square.


Triangular numbers -- Groups of 1, 3, or 6 dots can be arranged in a triangle and so these numbers are
called "triangular" numbers. If you've ever gone bowling, you'll recognize that 10 is also triangular.
A more natural example arises when we consider a group of people who meet for the first time and
everyone shakes hands with everyone else. The number of handshakes will be a triangular number. It
turns out that triangular numbers are the sum of consecutive integers.
1
3
6
10
=
=
=
=
1
1 + 2
1 + 2 + 3
1 + 2 + 3 + 4
To us this may just be an interesting relationship. To early Pythagoreans, it showed the mysterious
nature of the universe and the way in which numbers and shapes have esoteric relationships.
The Task
Your mission is to write a program that examines positive integers within a range that the user specifies and
categorizes them as follows:
 ODD or EVEN
 PRIME or COMPOSITE -- a prime number is an integer greater than one that is divisible only by itself
and one. Any number that is not "prime" is "composite". 1 is neither prime nor composite by definition
 PERFECT / ABUNDANT / DEFICIENT --- as described above
 SQUARE -- as described above
 TRIANGULAR -- as described above
You may assume that the user, a.k.a. your grader, will enter integers for this program.
The results are to be printed out in a well-formatted table. See the sample run below.
Implementation
Your program MUST include the following functions. Do NOT change the function names or parameters in the
function headers given below. Since this is a math-related project it's appropriate for us to use n as a variable
name for an integer. When a variable is holding the value of something, it's important to name that variable with
a meaningful name for what's being held, but just an integer, is okay to be called n. Similarly, it's okay to use a
and b. You may choose to use more functions if you wish, but these are sufficient. All predicate functions, by
definition, must return True or False. You have some flexibility in implementing the other functions.
Required Functions
def main():
# is the skeleton of the program
def printGreeting():
# explains the program to the user
def printTableHeading():
# prints the heading of the table
def isOdd(n):
# a predicate function that returns True if n is odd
# and returns False if not.
def isPrime(n):
# a predicate function that returns True if n is prime,
#and False if not
def checkForPerfect(n):
# classifies n as "perfect", "abundant" or "deficient".
# A different value is returned for each category.
def sumDivisors(n):
# returns the sum of the divisors of n.
def isDivisor(a, b):
# a predicate function that returns True if b is
# a divisor of a, and False if not.
def isSquare(n):
# returns True if n is a perfect square, False if not.
def isTriangular(n):
# returns True if n is a triangular number, False if not.
def printTableLine(n, odd, prime, perfect, square, triangular):
# prints the information for one number on one line of the table.
Sample Run
ecs225d-linux-01[257] python hw5.py
This program classifies positive integers as
Odd/Even, Prime/Composite, Perfect/Abundant/Deficient,
Square, and Triangular
You will now get to choose the range of positive integers that
you would like to see classified.
Start with which positive integer ?
Please enter an integer between 1 and 100000 : 1
End with which positive integer ?
Please enter an integer between 1 and 100000 : 30
Int
Classifications....................................
----------------------------------------------------------------------1
Odd
Neither
Deficient
Square
Triangular
2
Even
Prime
Deficient
3
Odd
Prime
Deficient
Triangular
4
Even
Composite
Deficient
Square
5
Odd
Prime
Deficient
6
Even
Composite
Perfect
Triangular
7
Odd
Prime
Deficient
8
Even
Composite
Deficient
9
Odd
Composite
Deficient
Square
10
Even
Composite
Deficient
Triangular
11
Odd
Prime
Deficient
12
Even
Composite
Abundant
13
Odd
Prime
Deficient
14
Even
Composite
Deficient
15
Odd
Composite
Deficient
Triangular
16
Even
Composite
Deficient
Square
17
Odd
Prime
Deficient
18
Even
Composite
Abundant
19
Odd
Prime
Deficient
20
Even
Composite
Abundant
21
Odd
Composite
Deficient
Triangular
22
Even
Composite
Deficient
23
Odd
Prime
Deficient
24
Even
Composite
Abundant
25
Odd
Composite
Deficient
Square
26
Even
Composite
Deficient
27
Odd
Composite
Deficient
28
Even
Composite
Perfect
Triangular
29
Odd
Prime
Deficient
30
Even
Composite
Abundant
Let's run it again with a different range of values
ecs225d-linux-01[258] python hw5.py
This program classifies positive integers as
Odd/Even, Prime/Composite, Perfect/Abundant/Deficient,
Square, and Triangular
You will now get to choose the range of positive integers that
you would like to see classified.
Start with which positive integer ?
Please enter an integer between 1 and 100000 : 20
End with which positive integer ?
Please enter an integer between 20 and 100000 : 50
Int
Classifications....................................
----------------------------------------------------------------------20
Even
Composite
Abundant
21
Odd
Composite
Deficient
Triangular
22
Even
Composite
Deficient
23
Odd
Prime
Deficient
24
Even
Composite
Abundant
25
Odd
Composite
Deficient
Square
26
Even
Composite
Deficient
27
Odd
Composite
Deficient
28
Even
Composite
Perfect
Triangular
29
Odd
Prime
Deficient
30
Even
Composite
Abundant
31
Odd
Prime
Deficient
32
Even
Composite
Deficient
33
Odd
Composite
Deficient
34
Even
Composite
Deficient
35
Odd
Composite
Deficient
36
Even
Composite
Abundant
Square
Triangular
37
Odd
Prime
Deficient
38
Even
Composite
Deficient
39
Odd
Composite
Deficient
40
Even
Composite
Abundant
41
Odd
Prime
Deficient
42
Even
Composite
Abundant
43
Odd
Prime
Deficient
44
Even
Composite
Deficient
45
Odd
Composite
Deficient
Triangular
46
Even
Composite
Deficient
47
Odd
Prime
Deficient
48
Even
Composite
Abundant
49
Odd
Composite
Deficient
Square
50
Even
Composite
Deficient
ecs225d-linux-01[259]
Although your output need not be identical to the above, all information (including the greeting) must be
present.
Other Requirements





Notice that in the primes column, 1 has the special case, 'Neither'. When the number is 1, you shouldn't
call the isPrime() function.
Notice in the second sample output, the prompt for the user to enter the end of the range, makes use of
the beginning value, so that the values of n always increase.
If the user fails to enter a value in the range specified by the prompt, your program should print an error
message and exit. You should remember using exit in lab 4 for a similar purpose.
Obviously the table should be generated by the results returned from the required functions and should
work for any range within 1 to 100000, inclusive
You should use print formatting to print the results in columns. You can left-justify a string in a
specified field width. Here's an example:
print "%-7s %-15s" % (str1, str2)
The left justification is caused by using a negative width.
Right justification uses a positive width.
Extra Credit
For 5 points of Extra Credit:
Instead of exiting when the user enters a bad value, keep prompting the user and getting new values until the
user enters a valid value. For grading purposes, if you decide to do the extra credit portion, you must submit a
README file that states you attempted the extra credit portion. A different grading script needs to be used to
handle the additional input. To create the README file, open a blank file in emacs called README and then
type a note to the grader letting him/her know that you did the extra credit. Then, you should submit this file
along with your hw5.py file.
Submitting your work
You must be logged into your account and in the same directory as the file you are trying to submit.
If you did not complete the extra credit, to submit your homework, type the following at the linux prompt:
submit cs201 HW5 hw5.py
If you did the extra credit portion, the submit command should be:
submit cs201 HW5 hw5.py README
To verify that your project was submitted, you can execute the following command at the Unix prompt. It will
show all files that you submitted in a format similar to the Unix 'ls' command.
submitls cs201 HW5
Download