CS 115 Homework 8 (Individual)
70 Points
Due Sunday, April 4 midnight.
Submit your work with the Canvas link.
Educational goals of this lab - verify that every student can
- use if statements and for loops
- write a boolean function
- do input validation
What does it mean for numbers to be friendly?
You calculate the sum of the divisors (or factors) of each number (including 1 and
including itself). Take the ratio of the sum to the number (the sum
will always be larger than the number) for each number.
Compare the two ratios. If they are equal the numbers are friendly.
The two numbers have to be greater than zero, otherwise you have division by zero.
- Example: 80, 200 are friendly.
The divisors of 80 are 1,2,4,5,8,10,16,20,40,80, the sum of these is 186.
The ratio of 186/80 = 2.325
The divisors of 200 are 1,2,4,5,8,10,20,25,40,50,100,200, the sum of these is 465
the ratio is 465/200 = 2.325
The ratios are the same, so the numbers 80 and 200 are friendly.
- Another example: 24, 25 are not friendly.
The divisors of 24 are 1, 2, 3, 4, 6, 8, 12, 24. The sum is 60.
the ratio is 60/24 = 2.5
The divisors of 25 are 1, 5, 25. The sum is 31.
The ratio is 31/25 = 1.34
The ratios are not equal, so 24 and 25 are not friendly.
- Sample run
Enter first number: 4
Enter second number: 25
4 and 25 are not friendly
- Another Sample run
Enter first number: -80
Please enter a number greater than zero
Enter first number: 80
Enter second number: 200
80 and 200 are friendly
- Another Sample run
Enter first number: -59
Please enter a number greater than zero
Enter first number: -8
Please enter a number greater than zero
Enter first number: 0
Please enter a number greater than zero
Enter first number: 159
Enter second number: -4
Please enter a number greater than zero
Enter second number: 35
159 and 35 are not friendly
See Wolfram page about
friendly numbers, for many more examples.
- Note: this is not the same concept as "amicable numbers". They are similar
but not the same.
(12 points) Test cases
- Fill these out before you write the program! As usual, put them in a comment at the bottom of your program file.
Note that each table is about one function. All you need to find is what the function returns, not what the
main does with it. The factors function returns the sum of the parameter's factors. The friendly function returns
a True or False value.
factors function
Case # | Description | Input(Parameter) | Expected Outcome |
1. | Number with 2 factors | 13 | 14 |
2. | Number with 3 factors | 25 | __A.__ |
3. | Number with 4 factors | 221 | __B.__ |
friendly function
Case # | Description | Input(Parameter) | Expected Outcome |
1. | Two friendly numbers | 80, 200 | ___C.__ |
2. | Two unfriendly numbers | 12, 15 | __D.__ |
main function
Case # | Description | Input from user | Expected Outcome |
1. | Valid inputs, friendly | 30, 140 | ___E.__ |
2. | Valid inputs, unfriendly | 120, 150 | __F.__ |
3. | Error condition, number <= 0 | 0 | reject with message, ask for another input |
- (10 points) Documentation/Design
- The functions factor and friendly definitions should have their separate 3 P's (prolog).
These are written from the point of view of the functions.
That means that pre-conditions always mention the parameters, any restrictions on them,
the data type of each. If any user input is done by the function, that should be mentioned too.
The post-conditions should discuss the return value if there is one, and what it means,
what data type it is, as well as any other effects of the function, such as if it prints
something to the screen.
- The friendly function should have in its prolog, an explanation of what 'friendly' means.
- The smaller functions do not discuss what they themselves are USED for, that is the job
of the calling function. They do not worry about how many times they are called or why.
- The prolog for the main
function is for the whole program, as it has always been. It includes
what is input from the outside world, what is output to the outside world.
The main's prolog can be placed either at the top of the file, as before, or put right above
the def main line.
- Each function needs some more comments. Any control structure needs a comment,
at least.
Implementation
- (15 points) Factors function
- Any integer has divisors or factors. A factor is a number that divides the number evenly.
The factors of 10 are 1, 2, 5, and 10. The factors of 13 are 1 and 13.
The factors of 25 are 1, 5 and 25.
The factors of 12 are 1, 2, 3, 4, 6 and 12.
- Write a function which accepts one parameter, an integer.
It returns the sum of the factors of the parameter.
factors(10) returns 18, factors(13) returns 14, factors(25) returns 31, factors(12) returns 28.
Boundary case: factors(1) returns 1, The function should assume that the parameter is greater than 0.
(a Pre-condition!)
- Yes, you need a loop and an accumulator.
- The % operator is very useful here - use it.
- Use a brute force approach, just try everything from 1 up to the number to see if it divides the
number with a remainder of zero.
- (13 points) Friendly function
- Write a friendly function that will accept two integers as parameters.
The function will return True if the two integers are "friendly", False if not.
- The friendly function should call the factors function (TWICE) to calculate the
sums of the factors of the two parameters.
It should calculate the ratios of the sums to the numbers itself. Then it
compares ratios and makes a decision.
- The function returns a Bool value, not a number. It does no input
from the keyboard nor output to the shell.
Remember because of structured programming, this function has exactly ONE return statement, no more.
(guaranteed One entrance, one exit)
Use an extra variable to hold the result of the comparison (True or False) and then return it at the bottom.
- (20 points) The main function
- Write a main function that will input two numbers from the user.
It will validate each one and give the user another chance if an input is
not greater than 0. (Input validation uses while loops!)
Then it calls
the friendly function with the two numbers. It will report the result as
"Yes they are friendly" or "No, they are not friendly".
- Submit your finished program with the Canvas link.