CPSC150A
Scientific Computing

Activity 7

Conditionals

Minimum

Write the function minimum(number_1, number_2) that returns the minimum of the two parameters, number_1 and number_2. The function should not use the built-in min function.

Clamp

Write the function clamp(number, minimum, maximum) that returns the result of clamping the specified number into the range [minimum, maximum]. That is, if number is below minimum, it returns minimum, if number is above maximum, it returns maximum, and otherwise it returns number. The function should not use the built-in functions min and max.

Perfect Numbers

A perfect number is a positive integer that is the sum of all of its positive divisors, excluding itself. For example, the divisors of 6 are 1, 2, 3, and 6, and 1 + 2 + 3 = 6. It is an open problem in mathematics as to whether there are a finite number of perfect numbers.

Details

Create a program that prompts the user for a positive integer and prints all of the perfect numbers less than the specified positive integer.

Example

Enter a positive integer: 1000
The perfect numbers less than 1000 are...
6
28
496

Hint

Writing this program will be much easier if you have a function that returns the sum of a number’s divisors. So, write the function sum_of_divisors(num):

  1. The function should have a loop that tests if every number between 1 and num is a divisor of num.
  2. A number is a divisor of another number if the remainder of the number divided by the divisor is 0.
  3. Use an accumulator to compute the sum of all of the divisors.

Test this function before writing the program that prints all of the divisors.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only programs that have perfect functionality will be accepted as complete.