Finding perfect numbers, let's put TheColi on the map!

Gonzo

Banned
Joined
Jul 29, 2012
Messages
6,181
Reputation
-167
Daps
26,856
Reppin
USA
Look at the numbers that have currently been found:

List of perfect numbers - Wikipedia, the free encyclopedia

I quickly made an application that finds perfect numbers but it takes an incredibly long time to find them.

This is the code:

double comp = 0, num = 2, test = num - 1;

double[] perfectList = new double[50];

boolean perf = false;

double stopRepeating = 0;

for ( int perfect = 0; perfect != 50;){

if (num % 10000 == 0 && stopRepeating != num && num < 100000){

System.out.println("\n\n" + num + " tested...\n");

stopRepeating = num;
}

if (num % 100000 == 0 && stopRepeating != num && num > 100000){

System.out.println("\n\n" + num + " tested...\n");

stopRepeating = num;

}

if (num % test == 0){

comp = comp + test;

}

if (comp > num){

num++;
test = num - 1;
comp = 0;

}

if (comp == num && test == 1){

perf = true;
}

if (perf){

perfect++;
System.out.print("****PERFECT NUMBER " + num + " FOUND****\n");
perfectList[perfect] = num;
num++;
test = num - 1;
comp = 0;
perf = false;
}

test--;


if (test == 0){

num++;
test = num - 1;

comp = 0;
}
}

System.out.println("Perfect Numbers: \n");

for (int count = 0; count < 50; count++){

System.out.println(perfectList[count] + "\n");

Let's be number 50! Can anyone help me alter my code or have better code so it may be more efficient to find the numbers? I made this in about 5 minutes so I know there is something better out there. It basically starts with x, y = x - 1, and through each iteration of the loop x is divided by y and y is subtracted by one. If the remainder of x / y = 0, then I add it to z, this adds up all of the numbers that return x / y = 0 and by the end of it all, see if z = x by the time y = 0.

I don't know why I found this so interesting but I do! All the coders cert gang assemble! :salute:
 
Top