Skip to content

Commit c3cd77d

Browse files
finished exercise 33
1 parent c214fc3 commit c3cd77d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/com/github/jonathanbirkey/chapter05/Exercise33.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22
* @author : Jonathan Birkey
33
* @mailto : [email protected]
44
* @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.
89
*/
910
package com.github.jonathanbirkey.chapter05;
1011

1112
public class Exercise33 {
1213
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+
}
1426
}
1527
}

0 commit comments

Comments
 (0)