Summer 2002: HW 5 07.10
Due date: 07.16
1. Objectives
Starting this week, I’m going to add a new element to the
homework assignments: Challenge problems, aimed at the
more restless programmers among you.
Their completion will be noted.
First skim, and then read the entire assignment before starting.
2. Perfect and not-so perfect numbers
A number is perfect if it equals the sum of its
divisors, including 1 but excluding itself. For example, 6 is perfect because 6
= 1 + 2 + 3. Similarly, 28 is perfect
because 28 = 1 + 2 + 4 + 7 + 14.
However, 4 is not perfect because 4 is not equal to 1 + 2. A number is deficient if the sum of its proper divisors is smaller than itself,
and abundant if that sum is
larger. Thus, 4 is deficient because 1
+ 2 < 4; the number 12, with proper divisors 1, 2, 3, 4, and 6 is abundant.
Part A:
Write a program which, given a positive integer by the user, reports if the
number is perfect, abundant, or deficient.
Save your program in a file called perfect.m.
Part B:
Use the results of Part
A to write a
new program that, given a positive number p by the user, reports all the
perfect numbers in the range [ 2, p ]. One way to think about this is that you have to put a modified
version of your old program in a loop.
Make sure your new program, however, prompts the user only once. Save this program in a file called allperfects.m.
This is your first example of a nested
loop; so don’t be scared if you are.
We’ll discuss it more on Monday.
3. Quirk-some Squares*
The number 3025
has a remarkable quirk: if you split its decimal representation in two and
square the sum of the numbers so obtained, you regain the original number:
(30 + 25)2 = 3025.
Leading zeros are acceptable, so 0001 = (00 + 01)2 is
also a quirk-some square (and the first).
Write a program called quirksome.m
that determines and prints all the 4 digit quirk-some squares.
See if your program can beat my best
time: 0.029651 seconds. If
yours runs faster than 5 seconds,
you’re doing well. To time
your program, use the commands tic and
toc. Can
you find the 6-digit numbers?
4. Submitting your work
Type your name, student ID, and the date at the top
of each M-file. Only perfect.m and allperfects.m are required. Print and sign each file. Hand the signed documents to the teaching
assistant at the beginning of lab on Tuesday 16 July 2002.
*Challenge problem