Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
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
42 changes: 42 additions & 0 deletions src/main/java/payapal/Perfection.java
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) {
Copy link
Copy Markdown

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

perf = new Perfection();
}
return perf;
}


public static boolean isPerfect(long candidate) {
boolean retVal;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, change the name to returnValue.

long[] divisors = GetDivisors(candidate);
int sum = 0;
for (int d = 0; d < 1000; d++)
{
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, change sum to long or use divisors[d].intValue() if possible.
Also, please use sum +=

}
if (sum == candidate)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, change candidate to Int.
candidate.intValue()

retVal = true;
return retVal;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, try to use return sum == candidate;, with this way, you could remove the retVal variable.

}


private static long[] GetDivisors(long candidate) {
long[] divisors = new long[];
int d = 0;
for (long i = 0; i < candidate; i++) {
long foo = candidate / i;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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;
}

}