In the second program you always print "... is a perfect number!"
Note that if you have a function with a return type other than void you should always make sure to return a value. In your first program the function never returns anything so the return type should probably be void. In the second program you only sometimes return a value, and the value that you return is the same as the argument. Is that really useful? Perhaps consider returning a boolean value, true (1) if the number is a perfect number and false (0) if the number is not a perfect number. Then you can check the return type to decide whether you should print "... is a perfect number!".
#include <stdio.h>
#include <stdlib.h>
int perfect(int number);
int main()
{
int num;
for (num = 1; num <= 1000; num++) { // num<=2000000 takes 5509.372 second
perfect(num);
}
return 0;
}
int perfect(int number)
{
int counter, sum = 0;
for (counter = 1; counter < number; counter++) {
if (number % counter == 0) {
sum += counter;
}
}
if (sum == number) {
printf("%d is a perfect number!\n", number);
}
}
#include <stdio.h>
#include <stdlib.h>
int perfect(int number);
int main()
{
int num;
for (num = 1; num <= 1000; num++) { // num<=2000000 takes 5509.372 second
printf("%d is a perfect number!\n", perfect(num));
}
return 0;
}
int perfect(int number)
{
int counter, sum = 0;
for (counter = 1; counter < number; counter++) {
if (number % counter == 0) {
sum += counter;
}
}
if (sum == number) {
return number;
}
}
#include <iostream>
int main()
{
constint n = 2000000; // Gives 6, 28, 496, 8128
// const int n = 40000000; // Needed for next one
staticunsignedlonglong sumProperFactors[1+n]{};
for ( int i = 1; 2 * i <= n; i++ )
{
for ( int j = 2 * i; j <= n; j += i ) sumProperFactors[j] += i;
}
for ( int i = 1; i <= n; i++ ) if ( sumProperFactors[i] == i ) std::cout << i << " ";
}