From c3cd77df1ec86d69d2c6c2d44590c4edd1e524b0 Mon Sep 17 00:00:00 2001 From: Jonathan Birkey Date: Thu, 7 Mar 2024 11:17:56 -0500 Subject: [PATCH] finished exercise 33 --- .../jonathanbirkey/chapter05/Exercise33.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/com/github/jonathanbirkey/chapter05/Exercise33.java b/src/com/github/jonathanbirkey/chapter05/Exercise33.java index 9310c76..000bb23 100644 --- a/src/com/github/jonathanbirkey/chapter05/Exercise33.java +++ b/src/com/github/jonathanbirkey/chapter05/Exercise33.java @@ -2,14 +2,26 @@ * @author : Jonathan Birkey * @mailto : jonathan.birkey@gmail.com * @created : 28Feb2024 - *

(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. + *

(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); + } + } } }