Skip to content

samuel review#6

Closed
leojdiaz wants to merge 2 commits into
mainfrom
samuel
Closed

samuel review#6
leojdiaz wants to merge 2 commits into
mainfrom
samuel

Conversation

@leojdiaz
Copy link
Copy Markdown
Owner

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

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

{
sum = sum + divisors[d];
}
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()



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.

int sum = 0;
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.

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

}
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.

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;
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.

@leojdiaz leojdiaz closed this Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants