Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Abundant Number in C ?
An abundant number (also known as excessive number) is a positive integer that is smaller than the sum of all its proper divisors. Proper divisors are all the positive divisors of a number except the number itself. For example, 12 is an abundant number because its proper divisors are 1, 2, 3, 4, and 6, and their sum (1+2+3+4+6 = 16) is greater than 12.
The difference between the sum of proper divisors and the number is called abundance. For the above example, abundance = 16 − 12 = 4.
Syntax
// Check if number is abundant
if (sum_of_proper_divisors > number) {
// Number is abundant
}
Algorithm
To check if a number is abundant −
- Find all proper divisors of the number
- Calculate the sum of these divisors
- Compare the sum with the original number
- If sum > number, then it's abundant
Example: Check if a Number is Abundant
This program checks whether a given number is abundant or not −
#include <stdio.h>
int main() {
int n = 12, sum = 0;
// Find all proper divisors and sum them
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
printf("Number: %d<br>", n);
printf("Sum of proper divisors: %d<br>", sum);
if (sum > n) {
printf("The number %d is an abundant number<br>", n);
printf("Abundance: %d<br>", sum - n);
} else {
printf("The number %d is not an abundant number<br>", n);
}
return 0;
}
Number: 12 Sum of proper divisors: 16 The number 12 is an abundant number Abundance: 4
Example: Optimized Approach
For better efficiency, we can find divisors up to the square root of the number −
#include <stdio.h>
#include <math.h>
int main() {
int n = 20, sum = 1; // 1 is always a proper divisor
printf("Number: %d<br>", n);
printf("Proper divisors: 1 ");
// Find divisors from 2 to sqrt(n)
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
sum += i;
printf("%d ", i);
// Add the corresponding divisor if it's different
if (i != n/i && n/i != n) {
sum += n/i;
printf("%d ", n/i);
}
}
}
printf("\nSum of proper divisors: %d<br>", sum);
if (sum > n) {
printf("The number %d is an abundant number<br>", n);
} else {
printf("The number %d is not an abundant number<br>", n);
}
return 0;
}
Number: 20 Proper divisors: 1 2 10 4 5 Sum of proper divisors: 22 The number 20 is an abundant number
Key Points
- The smallest abundant number is 12
- All even perfect numbers except 6 are abundant
- The optimized approach has O(?n) time complexity
- Always exclude the number itself when calculating proper divisors
Conclusion
An abundant number is identified when the sum of its proper divisors exceeds the number itself. The optimized algorithm using square root provides an efficient way to check this property for larger numbers.
