|
| 1 | +/** |
| 2 | + * A positive integer is called a perfect number if the sum of all its factors |
| 3 | + * (excluding the number itself, i.e., proper divisor) is equal to its value. |
| 4 | + * |
| 5 | + * For example, the number 6 is perfect because its proper divisors |
| 6 | + * are 1, 2, and 3, and 6=1+2+3; but the number 10 is not perfect |
| 7 | + * because its proper divisors are 1, 2, and 5, and 10≠1+2+5. |
| 8 | + * |
| 9 | + * A positive integer is called a deficient number if the sum of all its proper divisors |
| 10 | + * is less than its value. For example, 10 is a deficient number because 1+2+5 < 10; |
| 11 | + * while 12 is not because 1+2+3+4+6>12. |
| 12 | + * |
| 13 | + * Write a method called isPerfect(int posInt) that takes a positive integer, |
| 14 | + * and return true if the number is perfect. Similarly, write a method called |
| 15 | + * isDeficient(int posInt) to check for deficient numbers. |
| 16 | + * |
| 17 | + * Using the methods, write a program called PerfectNumberList that prompts user |
| 18 | + * for an upper bound (a positive integer), and lists all the perfect numbers |
| 19 | + * less than or equal to this upper bound. It shall also list all the numbers |
| 20 | + * that are neither deficient nor perfect. The output shall look like: |
| 21 | + * |
| 22 | + * Enter the upper bound: 1000 |
| 23 | + * These numbers are perfect: |
| 24 | + * 6 28 496 |
| 25 | + * [3 perfect numbers found (0.30%)] |
| 26 | + * |
| 27 | + * These numbers are neither deficient nor perfect: |
| 28 | + * 12 18 20 24 30 36 40 42 48 54 56 60 66 70 72 78 80 ...... |
| 29 | + * [246 numbers found (24.60%)] |
| 30 | + * |
| 31 | + */ |
| 32 | + |
| 33 | +package javaexercises.numbertheory; |
| 34 | + |
| 35 | +import java.util.Scanner; |
| 36 | + |
| 37 | +public class PerfectNumberList { |
| 38 | + |
| 39 | + public static void main(String[] args) |
| 40 | + { |
| 41 | + PerfectNumberList aNumberTheoryUtil = new PerfectNumberList(); |
| 42 | + |
| 43 | + System.out.print("Enter the upper bound (positive integer): "); |
| 44 | + Scanner in = new Scanner(System.in); |
| 45 | + if ( ! in.hasNextInt()) { |
| 46 | + System.out.println("Upper bound not valid. Please try again."); |
| 47 | + return; |
| 48 | + } |
| 49 | + int upperBound = in.nextInt(); |
| 50 | + |
| 51 | + if (upperBound < 0) { |
| 52 | + System.out.println("Upper bound is not positive. Please try again."); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + System.out.println("These numbers are perfect:"); |
| 57 | + int countPerfectNumbers = 0; |
| 58 | + for (int i = 1; i <= upperBound; i++) { |
| 59 | + if ( ! aNumberTheoryUtil.isPerfect(i)) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + System.out.printf("%d ", i); |
| 63 | + countPerfectNumbers++; |
| 64 | + } |
| 65 | + System.out.printf("\n[%1$d perfect numbers found (%2$.2f%%)]\n", |
| 66 | + countPerfectNumbers, ((double)100 * countPerfectNumbers/upperBound) |
| 67 | + ); |
| 68 | + |
| 69 | + System.out.println(); |
| 70 | + |
| 71 | + System.out.println("These numbers are neither deficient nor perfect:"); |
| 72 | + int countDeficientNumbers = 0; |
| 73 | + for (int i = 1; i <= upperBound; i++) { |
| 74 | + if ( aNumberTheoryUtil.isDeficient(i) || aNumberTheoryUtil.isPerfect(i)) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + System.out.printf("%d ", i); |
| 78 | + countDeficientNumbers++; |
| 79 | + } |
| 80 | + System.out.printf("\n[%1$d numbers found (%2$.2f%%)]\n", |
| 81 | + countDeficientNumbers, ((double)100 * countDeficientNumbers/upperBound) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + // return true if the number is perfect |
| 86 | + private boolean isPerfect(int posInt) |
| 87 | + { |
| 88 | + int sumDivisors = 0; |
| 89 | + for (int i = 1 ; i < posInt; i++) { |
| 90 | + sumDivisors += (posInt%i == 0) ? i : 0; |
| 91 | + } |
| 92 | + return (sumDivisors == posInt); |
| 93 | + } |
| 94 | + |
| 95 | + // return true if the number is deficient |
| 96 | + private boolean isDeficient(int posInt) |
| 97 | + { |
| 98 | + int sumDivisors = 0; |
| 99 | + for (int i = 1 ; i < posInt; i++) { |
| 100 | + sumDivisors += (posInt%i == 0) ? i : 0; |
| 101 | + } |
| 102 | + return (sumDivisors < posInt); |
| 103 | + } |
| 104 | +} |
0 commit comments