I created a problem and solved it but i noticed that the outputting is kinda slow. Can someone here make a faster algo for this problem than mine pls?
This problem will output all perfect numbers between 1 and int max.
A perfect number is an integer that is equal to the sum of all of its factors other than itself..
e.g.
1 + 2 + 3 = 6
other perfect numbers are 28 and 496
PHP Code:
#include<stdio.h>
#include<conio.h>
int main()
{
long num, ctr;
long curtotal, max;
clrscr();
printf( "Input:" );
scanf( "%ld", &max );
printf( "\nOutput:\n\n" );
for( num = 1; num < max; num++ ){
for( ctr = 1, curtotal = 0; ctr < num; ctr++ ){
if( ( num % ctr ) == 0 )
curtotal += ctr;
}
if( curtotal == num )
printf( "%ld\n", num );
}
getch();
return 0;
}