Conversation
| private static Perfection perf; | ||
|
|
||
| public synchronized static Perfection getPerf() { | ||
| if (perf == null) { |
There was a problem hiding this comment.
Please, change for a ternary if.
return perf != null ? perf : new Perfection();
| { | ||
| sum = sum + divisors[d]; | ||
| } | ||
| if (sum == candidate) |
There was a problem hiding this comment.
Please, change candidate to Int.
candidate.intValue()
|
|
||
|
|
||
| public static boolean isPerfect(long candidate) { | ||
| boolean retVal; |
There was a problem hiding this comment.
Please, change the name to returnValue.
| long[] divisors = GetDivisors(candidate); | ||
| int sum = 0; | ||
| for (int d = 0; d < 1000; d++) | ||
| { |
There was a problem hiding this comment.
Please, use the braces in the same line as the code.
| int sum = 0; | ||
| for (int d = 0; d < 1000; d++) | ||
| { | ||
| sum = sum + divisors[d]; |
There was a problem hiding this comment.
Please, change sum to long or use divisors[d].intValue() if possible.
Also, please use sum +=
| } | ||
| if (sum == candidate) | ||
| retVal = true; | ||
| return retVal; |
There was a problem hiding this comment.
Maybe, try to use return sum == candidate;, with this way, you could remove the retVal variable.
| 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.
Note: try to avoid long divide operations, because you will lose precision. Try to use Bigdecimal.
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