Skip to content

by facundo#7

Closed
leojdiaz wants to merge 2 commits into
mainfrom
cazzaro
Closed

by facundo#7
leojdiaz wants to merge 2 commits into
mainfrom
cazzaro

Conversation

@leojdiaz
Copy link
Copy Markdown
Owner

@leojdiaz leojdiaz commented Sep 20, 2021

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

@leojdiaz leojdiaz requested a review from IngCazzaro September 20, 2021 17:57

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

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

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

}
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

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.

sum += divisors[d] is more redeable

@leojdiaz leojdiaz closed this Sep 21, 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