Finding Perfect Numbers in C: A Complete Guide
Understanding Perfect Numbers
A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. For example, 6 is a perfect number because its divisors (1, 2, 3) sum to 6 (1+2+3=6). Other perfect numbers include 28, 496, and 8128.
Perfect numbers have interesting properties in number theory. They are always even numbers, and they are closely related to the sum-of-divisors function in mathematics.
Approach to Identify Perfect Numbers
To determine if a number is perfect, we need to find all its proper divisors and check if their sum equals the number itself. The algorithm involves:
- Iterating through numbers from 1 to 999
- For each number, finding all its proper divisors
- Summing these divisors
- Checking if the sum equals the original number
Implementation Without Functions
#include
int main() {
int number, divisor, sum;
for (number = 1; number < 1000; number++) {
sum = 0;
// Find all divisors and sum them
for (divisor = 1; divisor < number; divisor++) {
if (number % divisor == 0) {
sum += divisor;
}
}
// Check if the number is perfect
if (sum == number) {
printf("%d its factors are ", number);
// Print all divisors
for (divisor = 1; divisor < number; divisor++) {
if (number % divisor == 0) {
printf("%d ", divisor);
}
}
printf("\n");
}
}
return 0;
}
Implementation With Functions
#include
// Function to check if a number is perfect
int isPerfect(int num) {
int total = 0;
// Calculate sum of proper divisors
for (int i = 1; i < num; i++) {
if (num % i == 0) {
total += i;
}
}
// Return 1 if perfect, 0 otherwise
return (total == num) ? 1 : 0;
}
// Function to print divisors of a number
void printDivisors(int num) {
for (int i = 1; i < num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
}
int main() {
int current;
// Check all numbers from 1 to 999
for (current = 1; current < 1000; current++) {
if (isPerfect(current)) {
printf("%d its factors are ", current);
printDivisors(current);
printf("\n");
}
}
return 0;
}
Optimizing the Divisor Calculation
The above implementations can be optimized. Notice that divisors come in pairs. For example, for number 28, divisors are 1, 2, 4, 7, 14. We only need to check up to the square root of the number to find all divisors:
#include
#include
int isPerfect(int num) {
if (num == 1) return 0; // 1 has no proper divisors
int total = 1; // 1 is a proper divisor for all numbers > 1
int sqrt_num = sqrt(num);
for (int i = 2; i <= sqrt_num; i++) {
if (num % i == 0) {
total += i;
int complement = num / i;
if (complement != i) {
total += complement;
}
}
}
return (total == num) ? 1 : 0;
}
void printDivisors(int num) {
printf("1 "); // 1 is always a divisor
int sqrt_num = sqrt(num);
for (int i = 2; i <= sqrt_num; i++) {
if (num % i == 0) {
printf("%d ", i);
int complement = num / i;
if (complement != i) {
printf("%d ", complement);
}
}
}
}
int main() {
for (int current = 2; current < 1000; current++) {
if (isPerfect(current)) {
printf("%d its factors are ", current);
printDivisors(current);
printf("\n");
}
}
return 0;
}