Conversation
IngCazzaro
reviewed
Sep 20, 2021
|
|
||
| private static Perfection perf; | ||
|
|
||
| public synchronized static Perfection getPerf() { |
There was a problem hiding this comment.
synchronized is not necesary, the static singleton is thread safe
| long[] divisors = new long[]; | ||
| int d = 0; | ||
| for (long i = 0; i < candidate; i++) { | ||
| long foo = candidate / i; |
There was a problem hiding this comment.
you can simplify this using %, if (candidate % i == 0 ) is a divisor because the rest is 0
| public static boolean isPerfect(long candidate) { | ||
| boolean retVal; | ||
| long[] divisors = GetDivisors(candidate); | ||
| int sum = 0; |
There was a problem hiding this comment.
Should be a long, you are adding long and it may overflow
| } | ||
| if (sum == candidate) | ||
| retVal = true; | ||
| return retVal; |
There was a problem hiding this comment.
you can return the result of the comparasion and do that 3 lines in one
| int sum = 0; | ||
| for (int d = 0; d < 1000; d++) | ||
| { | ||
| sum = sum + divisors[d]; |
There was a problem hiding this comment.
sum += divisors[d] is more redeable
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Definition of a Perfect Number:
In mathematics a perfect number is defined as an integer which is the sum of its proper
positive divisors; that is, the sum of the positive divisors not including the number itself.
Some examples of perfect numbers are:
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248