This is the second homework assignment. Your goal in this assignment is to write functions that perform a calculation and return a value.

Practical information#

The assignment is due on Saturday, 11 of March (week 3), at 11:55pm (Canberra time). To submit your solution, upload a single Python file via Wattle. Here is the assignment submission link.

In addition to submitting your solution, you will attend your lab in the same week (semester week 4). In the lab, your tutor may ask you some questions about your solution, and give you feedback if there is anything you need to improve. This discussion is NOT part of the assessment.

The homework is individual. You must write your own solution, and you are expected to be able to explain every aspect of it. Also remember that you are not allowed to share your solution (code) with other students; this includes posting it (or parts of it) to the discussion forum, or to any other on-line forum.

If you have followed the lectures and worked through the exercises in lab 2, the assignment should not take more than an hour or two to complete.

The problem#

This homework asks you to compute simple arithmetic properties of a natural number (a positive integer), which were studied already in the age of Antiquity. Every natural number can be characterised by its factors, or dividers — natural numbers which divide the original without a remainder. In this study, the factors of a number N are greater or equal 1, and smaller than N itself. That is, the number \(N=1\) has no factors, but any \(N > 1\) has 1 among its factors. With such definition, for example, a number \(N>1\) that has only one factor 1, is a prime number.

Apart from primes, there are other numbers with interesting properties. A number is called perfect if the sum of all its factors is equal to that number. An example of a perfect number is 28; it has the factors $1, 2, 4, 7, 14$ and their sum is:

1 + 2 + 4 + 7 + 14 = 28.

The next perfect number after 28 is 496, that was computed by (as the legend says) Pythagoras. Perfect numbers are quite rare, and they are not easy to compute. In the range 1..1,000,000, for example, there are only four perfect numbers 6, 28, 496, 8128 (in fact, the next perfect number after 8128 is 33,550,336). Because of this rarity, finding ever increasing perfect numbers is a non-trivial computational task (in terms of computational cost, not algorithmically).

Any natural number can be characterised as either:

  • perfect (sum of all factors equals the number)
  • deficient (sum of all factors is less than the number)
  • excessive (sum of all factors is greater than the number)

In this homework, you are asked to compute the number of deficient and excessive numbers which are smaller than a given natural number. Namely, there are three function prototypes which you will have to implement:

  • sum_factors(n) – computes the sum of all factors of the natural number n,
  • excessives(N) – computes how many excessive numbers smaller than the natural number N there are,
  • deficients(N) – computes how many deficient numbers that are smaller than the natural number N there are.

All three prototypes are to be found in the template file imperfects.py, which also contains three test functions to check the correctness of each function implementations:

  • test_sum_factors()
  • test_deficients()
  • test_excessives()

If you run each of them separately, the corresponding function will be called several times (eg, sum_factors will be called during the execution of test_sum_factors), and correctness of the value returned by it will be tested. If all returned values are what they are supposed to be, the message all tests passed will be printed, indicating that the function works as intended. Remember, that even passing all tests does not guarantee the absolute functional correctness. Testing is good for finding errors, it’s not a method of proving that there are no errors. Therefore, you still need to pay attention to logical correctness of your solution.

Note: the function sum_factors should only calculate and return the sum (an integer number), not compute and return the factors themselves (which could be done by collecting all found factors into a list, but we do not ask your to use lists in this homework).

Testing#

The skeleton file has three testing functions:

  • test_sum_factors() This function runs a number of tests of your function, and produces an error message if any of the tests fail. If all tests are OK, it will print the message all tests for sum_factors passed.
  • test_deficients() and test_excessives() are two functions which run several tests each to check that the corresponding function deficients(N) or excessives(N) returns correct values. If all the tests are OK, the functions print similar messages.

Marking#

What to submit

You should submit the file named imperfects.py. Download the prototype file imperfects.py, implement three required function prototypes, and upload this file as your solution using the assignment submission link on Wattle. Do not change the file name by adding your name or University ID (you will be submitting into your personal directory, and there is no risk that your file will be confused with someone else’s). Do not forget to inlcude your name and Uni ID (lines 3 and 4 in the prototype file).

The file that you submit must meet the following requirements:

  • It must be syntactically correct Python code.
  • Like the file you downloaded, it must contain only function definitions; comments, including docstrings (if they are used appropriately) are, of course, OK to include. Anything that is not a function definition will be ignored when we check your submission.
  • You should not modify the testing functions.

As mentioned above, you should also attend the following lab (in week 4) and discuss your solution with a tutor. This discussion is NOT part of the assessment. Questions which can be discussed are the following:

  • Can you download the file that you submitted from Wattle?
  • Can you run that file in the Python interpreter (using an IDE of your choice)?
  • If the file has syntax errors, can you use the error messages from the interpreter or IDE to identify where the syntax errors are?
  • Does your submitted file meet the requirements stated above? Does it contain anything that is not a function definition? If so, can you point it out?
  • Do your functions pass all the tests run by the unmodified test functions?
  • Do your functions correctly compute and return the number of deficient and excessive numbers smaller than N?
  • Do your functions always return a value of the correct type?
  • Can you think of any other test cases that should be used to test your functions, in addition to or instead of those in test functions?
  • What is the difference between the print function call and the return statement?

In marking this assignment we will consider the following:

  • Does your submitted file satisfy the requirements specified above?
  • Does your implementation of the sum_factors, deficients and excessives functions compute the correct value for all argument values?
  • Your ability to use the tools (e.g., the IDE or Python interpreter), your understanding of Python’s error messages, and your understanding of the solution, as demonstrated in your discussion with the tutor.

The assignment is worth 2% of your final mark.

bars search times arrow-up