-
Notifications
You must be signed in to change notification settings - Fork 2
samuel review #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,10 @@ | ||
| JavaCodeReview | ||
| This class determine if a number is perfection or not. | ||
|
|
||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package main.java.payapal; | ||
|
|
||
| public class Perfection { | ||
|
|
||
| private static Perfection perf; | ||
|
|
||
| public synchronized static Perfection getPerf() { | ||
| if (perf == null) { | ||
| perf = new Perfection(); | ||
| } | ||
| return perf; | ||
| } | ||
|
|
||
|
|
||
| public static boolean isPerfect(long candidate) { | ||
| boolean retVal; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, change the name to |
||
| long[] divisors = GetDivisors(candidate); | ||
| int sum = 0; | ||
| for (int d = 0; d < 1000; d++) | ||
| { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, use the braces in the same line as the code. |
||
| sum = sum + divisors[d]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, change sum to |
||
| } | ||
| if (sum == candidate) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, change candidate to Int. |
||
| retVal = true; | ||
| return retVal; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, try to use |
||
| } | ||
|
|
||
|
|
||
| private static long[] GetDivisors(long candidate) { | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: try to avoid long divide operations, because you will lose precision. Try to use Bigdecimal. |
||
| if (foo * i == candidate) { | ||
| divisors[d] = i; | ||
| d = d + 1; | ||
| } | ||
| } | ||
| return divisors; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, change for a ternary if.
return perf != null ? perf : new Perfection();