File tree 1 file changed +16
-4
lines changed
src/com/github/jonathanbirkey/chapter05
1 file changed +16
-4
lines changed Original file line number Diff line number Diff line change 2
2
* @author : Jonathan Birkey
3
3
4
4
* @created : 28Feb2024
5
- * <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from
6
- * 101 to 2100, separated by exactly one space. Also display the number of leap years in this
7
- * period.
5
+ * <p>(Perfect number) A positive integer is called a perfect number if it is equal to the sum
6
+ * of all of its positive divisors, excluding itself. For example, 6 is the first perfect number
7
+ * because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers <
8
+ * 10,000. Write a program to find all these four numbers.
8
9
*/
9
10
package com .github .jonathanbirkey .chapter05 ;
10
11
11
12
public class Exercise33 {
12
13
public static void main (String [] args ) {
13
- // TODO: solve
14
+
15
+ for (int i = 1 ; i < 10000 ; i ++) {
16
+ int sumPosDivisors = 0 ;
17
+ for (int j = 1 ; j <= (int ) i / 2 ; j ++) {
18
+ if (i % j == 0 ) {
19
+ sumPosDivisors += j ;
20
+ }
21
+ }
22
+ if (i == sumPosDivisors ) {
23
+ System .out .printf ("Perfect number: %d\n " , i );
24
+ }
25
+ }
14
26
}
15
27
}
You can’t perform that action at this time.
0 commit comments