cs-078 — Perfect Numbers
| Field | Value |
|---|---|
| id | cs-078 |
| difficulty | Foundational |
| topics | brute force, divisors |
| lecture | lecture-20 |
| estimated time | 5-minute attempt + 10-minute discussion |
| prerequisites | lectures 1-2 (problem framing, precise pseudocode) |
| objective | recognise the pattern, translate it into precise steps, and trace it correctly |
| solution | held by the instructor; discussed after the attempt |
Problem
Find all perfect numbers below 10000 (equal to sum of proper divisors). Brute force with a smarter divisor bound; measure the improvement.
Your deliverable is the enumerated answer, the space count, and the pruning rule with its measured effect. If the problem is ambiguous anywhere, state the assumption you are making and carry on — a stated assumption is part of a good answer.
Examples
- Input: perfect numbers below 10000 (sum of proper divisors equals n): 6, 28, 496, 8128
- Output: naive O(n^2); divisor bound sqrt(n) makes it ~O(n sqrt n) - measure both
Solve for the worked instance first. Then ask what would change if one number, name, or condition were replaced by something stranger.
What to notice
- Divisors pair around sqrt(n); check d and n/d together - one loop to the square root.
- Brute force is a legitimate algorithm when the space is honest about its size.
- Pruning is not cheating; it is arithmetic applied early.
Discussion starter
After the solution is revealed, the class spends five minutes on this prompt: "Double the input size: is this still acceptable? Show the arithmetic."
Constraints and assumptions
- Difficulty: Foundational (early-semester techniques assumed: precise statements, traces, simple loops).
- Topics: brute force, divisors.
- Classroom-scale inputs unless the problem says otherwise.
- Time-box: about five minutes; naming the pattern and stating the assumption is already a pass, completing the calculation is the stretch.
- Optional stretch (not required): describe an alternative approach and say when it beats the obvious one.
Hints and extension
- First nudge: identify the pattern the worked instance uses before computing anything.
- If stuck after three minutes, write the input/output line you expect and trace the smallest example by hand.
- After the reveal, attempt the extension below before the next lecture.
- Extension challenge: Bound the candidate space first; then decide whether enumeration is honest.