// Check weather a number is a perfect number // Variant 1: Brute force // g++ -Wno-deprecated PerfectNum1.cc #include #include main() { long long int a, // number to investigate i,n, // loop index and last index sum_a; // sum of all factors cout << endl << " Input a = "; cin >> a; cout << endl; n = a/2; // potentially largest factor sum_a = 0; for ( i=1; i<=n; i++ ) { if ( ( a % i ) == 0 ) // is var. i a factor? { sum_a += i; } } cout << " a = " << a << " is "; if ( sum_a != a) // sum of factors not equal number a ? { cout << "not "; } cout << "a perfect number" << endl << endl; } /* Perfect numbers are: 6 28 496 8128 33550336 // Laptop: user 0m0.545 / 0m1.736s fuer 4/8 Byte-int 8589869056 // Here, we need 8 Byte integers (long long int) // Laptop: user 7m25.344s */