Find if a number is equal to the sum of its divisors A number is perfect if it is equal to the sum of all its divisors. For example divisors of 6 are 1, 2, 3. I want to read numbers, and for each number to print 1 if it is perfect and 0 if it's not perfect. Numbers are smaller than 10 ^ 19. How would I do it faster? My version:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
double perfectCheck( double number )
{
    double i, sum = 0;
    for( i = 1; i &lt;= number / 2; i++ )
        if(( int )( number / i ) * i == number )
            sum += i;
    return sum;
}

int main()
{
    double n, i, m;
    scanf( "%lf", &amp;n );
    for( i = 0; i &lt; n; i++ )
    {
        scanf( "%lf", &amp;m );
        if( perfectCheck( m ) == m )
            printf( "1 " );
        else
            printf( "0 " );
    }
    return 0;
}


Input:

5
28 7 8 9 6


Output:

1 0 0 0 1


I use C89.
Finding divisors faster

Instead of checking all numbers until number / 2,
it's enough to search until sqrt(number),
and count the divisor pairs.
For example, in the case of 6,
initialize sum = 1, because 1 will be always part of the sum,
and then when you see 2, also add 6 / 2 to the sum.
(You can get sqrt with #include "math.h".)
Also,
you can break out of the loop when sum exceeds the target value.

There is one more important point that can slow your implementation down,
is that all the variables are double instead of int.
This is not only unnatural for the given task,
floating point math is expensive in general.
So simply rewriting the program so that number, i, m, n are all integers, will make a big difference.

Make perfectCheck return boolean

Currently perfectCheck returns the sum of divisors.
You could make it return a bool:

return sum == number;


You can get the bool type in C99+ with #include "stdbool.h".

And then you can write the main function a bit simpler:

for (i = 0; i &lt; n; i++)
{
    scanf("%d", &amp;m);
    printf("%d ", perfectCheck(m));
}