Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help Debug My Perfect Number Program

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Im trying to write a program that finds all perfect numbers up to 1 billion...
Its not working... so can someone help fix it?


#include <stdio.h>
#include <conio.h>
long x, loops, i, y, a, num, sum;

/*function prototype*/
long Perfect(int x);

int main()
{
/*Calls Function Perfect(x)*/
a = Perfect(x);
num = a;

/*finds divisors of perfect numbers*/
for(i=1; i <= num/2; i++)
{
if(!(num % i))
sum += i;
}

printf(&quot;%ld&quot;, sum);

return 0;
}

/*Return Function*/
long Perfect(int x)
{
loops = 0;

while (loops < 1000000000)
{
if ((2^x)-1 % 1)
y = (2^(x-1))*((2^x)-1);
loops++;
}
return y;

}

/*End of Program*/
 
Hi NewBeePersonThing,

Here's some code that finds 7 perfect numbers (last one is 137438691328). Maybe if you increase x in the main loop you can find more.
Hope this helps...

hilarity.



#include <stdio.h>
#include <math.h>

int main(void) {
int x; double n;
for(x=2; x < 20; x++)
if (isPrime(n=pow2(x)-1))
printf(&quot;Perfect number = %20.0f\n&quot;, pow2(x-1)*n);
return 0;
}

int isPrime(double x) {
double i=2;
while(!((x/i)-abs(x/i)<0.00000001)) i++;
return (i==x);
}

int pow2(int x) {
int i, result=1;
for(i=0; i<x; result*=2, i++);
return(result);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top