-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c214fc3
commit c3cd77d
Showing
1 changed file
with
16 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,26 @@ | |
* @author : Jonathan Birkey | ||
* @mailto : [email protected] | ||
* @created : 28Feb2024 | ||
* <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from | ||
* 101 to 2100, separated by exactly one space. Also display the number of leap years in this | ||
* period. | ||
* <p>(Perfect number) A positive integer is called a perfect number if it is equal to the sum | ||
* of all of its positive divisors, excluding itself. For example, 6 is the first perfect number | ||
* because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers < | ||
* 10,000. Write a program to find all these four numbers. | ||
*/ | ||
package com.github.jonathanbirkey.chapter05; | ||
|
||
public class Exercise33 { | ||
public static void main(String[] args) { | ||
// TODO: solve | ||
|
||
for (int i = 1; i < 10000; i++) { | ||
int sumPosDivisors = 0; | ||
for (int j = 1; j <= (int) i / 2; j++) { | ||
if (i % j == 0) { | ||
sumPosDivisors += j; | ||
} | ||
} | ||
if (i == sumPosDivisors) { | ||
System.out.printf("Perfect number: %d\n", i); | ||
} | ||
} | ||
} | ||
} |