CS 117, Spring 1998

Assignment 3: Numbers

Due 11:10 AM, 4/15/98

Please work on your own for this assignment. You may discuss the problems with other people, but write your own code. When you are done with this assignment, please submit it using the Homework Submission Program.

Your assignment is to write a pair of functions, both described below. Hand in only the function and the procedure, not the full program. The grader will copy your functions into her own testing program.

Part I: Perfect numbers

Number theorists usually start their lives as number fanatics. They'll do all sorts of weird things to numbers in search of interesting patterns. One of the many such weird things they have done over the centuries is to add up the factors of integers. For example, the sum of the factors of 12 is 1 + 2 + 3 + 4 + 6 = 16, while the sum of the factors of 8 is 1 + 2 + 4 = 7. (Note that we're only adding up the "proper" factors--that is, all the factors other than the number itself. Also, when I talk about "numbers" and "factors" in this context, I am talking only about positive integers and their positive factors.)

After number theorists have played a game like this for a while, they start naming things. Any number whose factors sum to something larger than the number itself is called abundant, and any number whose factors sum to something smaller than the number are called deficient. Thus, 12 is abundant, while 8 is deficient.

A number whose factors add up to the number itself is something special. The Pythagoreans of the sixth century BC, who were fond of attributing mystical significance to unusual numerical patterns, called such numbers perfect. The number 6 is the smallest perfect number, and 28 is next. Check them out to make sure you understand how numerical perfection works.

Part II: Reducing fractions

When you work with real numbers in computer programs, you can end up with lots of roundoff errors, even when you work with simple numbers like 1/3 or 1/10. One way to avoid roundoff error is to work only with fractions--that is, with pairs of integers (numerator and denominator). If you write a program to work with fractions, you'll want to be able to reduce your fractions to keep your numerators and denominators as small as possible. For example, you would rather work with numerator = 3 and denominator = 4 than with numerator = 1413 and denominator = 1884.

Your tasks

  1. Implement the function IsPerfect, adhering to the following interface:
    
    /////////////////////////////////////////////////////////
    //	IsPerfect returns true if its parameter is a
    //	perfect number, and false otherwise.
    /////////////////////////////////////////////////////////
    
    bool IsPerfect( int n );
    
  2. Implement the procedure ReduceFraction, adhering to the following interface:
    
    /////////////////////////////////////////////////////////
    //	ReduceFraction takes the given fraction n/d, and
    //	reduces it to lowest terms, returning the reduced
    //	numerator and denominator via the reference parameters
    //	n and d.
    /////////////////////////////////////////////////////////
    
    void ReduceFraction( int& n, int& d );
    

Start early, keep in touch, and have fun.



Jeff Ondich, Department of Mathematics and Computer Science, Carleton College, Northfield, MN 55057, (507) 646-4364, jondich@carleton.edu