Cover page images (keyboard)

HW5 - Functions


due on Sunday, 10/17/10, before 11:59 PM

Sue Evans

Hit the space bar for next page

Homework Requirements




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 :

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:

You may assume that the user, a.k.a. your grader, will enter appropriate 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.

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

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.

Submitting your work

You must be logged into your account and in the same directory as the file you are trying to submit.

To submit your homework, type the following at the linux prompt:

submit cs201 HW5 hw5.py

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