-
Notifications
You must be signed in to change notification settings - Fork 2
by facundo #7
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
by facundo #7
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,8 @@ | ||
| JavaCodeReview | ||
| 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; | ||
| long[] divisors = GetDivisors(candidate); | ||
| int sum = 0; | ||
|
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. Should be a long, you are adding long and it may overflow |
||
| for (int d = 0; d < 1000; d++) | ||
| { | ||
| 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. sum += divisors[d] is more redeable |
||
| } | ||
| if (sum == candidate) | ||
| 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. you can return the result of the comparasion and do that 3 lines in one |
||
| } | ||
|
|
||
|
|
||
| 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. you can simplify this using %, if (candidate % i == 0 ) is a divisor because the rest is 0 |
||
| 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.
synchronized is not necesary, the static singleton is thread safe