Loading...
Loading...
A number theory club sorts numbers by the sum of their proper divisors (all divisors except the number itself). If the sum equals the number it is PERFECT (6 = 1 + 2 + 3), if it is larger it is ABUNDANT, and if it is smaller it is DEFICIENT.
Write static long sumOfProperDivisors(int n) that only loops up to √n, and use it to label each number.
PERFECT, ABUNDANT or DEFICIENT.Example
Input
3 6 12 8
Output
PERFECT ABUNDANT DEFICIENT
6: 1+2+3 = 6. 12: 1+2+3+4+6 = 16. 8: 1+2+4 = 7.
Try the problem first. Open one hint at a time.
Divisors come in pairs: if d divides n, so does n / d. Loop d from 2 while d * d <= n.
Add both d and n / d, but only once when they are equal.
Start the sum at 1 for n > 1 (1 is always a proper divisor), and return 0 for n = 1.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor