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
9 changes: 8 additions & 1 deletion README.md
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
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() {
Copy link
Copy Markdown

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

if (perf == null) {
perf = new Perfection();
}
return perf;
}


public static boolean isPerfect(long candidate) {
boolean retVal;
long[] divisors = GetDivisors(candidate);
int sum = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

}