Python Topics
- Python Intro
- Python String Methods
- Python Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Perfect Number
- Perfect Square
- Power of 2
- Power of 3
- Pronic Number
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- Python Star Pattern
- Python Number Pattern
- Python Alphabet Pattern
Python Program to Check Abundant Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, understanding and identifying special types of numbers is a fascinating exploration. One such category is abundant numbers.
An abundant number, also known as an excessive number, is a positive integer that is smaller than the sum of its proper divisors (excluding itself).
In this tutorial, we'll delve into a Python program designed to check whether a given number is an abundant number or not.
The program will employ logic to calculate the sum of the proper divisors of a number and determine if it exceeds the number itself.
π Example
Let's take a look at the Python code that achieves this functionality.
# Function to check if a number is abundant
def is_abundant(num):
# Start with 1 as every number is divisible by 1
divisor_sum = 1
# Iterate from 2 to the square root of the number
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
divisor_sum += i
if i != num // i:
divisor_sum += num // i
# If the sum of divisors exceeds the number, it is abundant
return divisor_sum > num
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 12
# Check if the number is abundant
if is_abundant(number):
print(f"{number} is an abundant number.")
else:
print(f"{number} is not an abundant number.")
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the if __name__ == "__main__": block.
12 is an abundant number.
Run the script to check if the number is an abundant number.
π§ How the Program Works
- The program defines a function is_abundant that takes a number as input and returns True if the number is abundant and False otherwise.
- Inside the function, it iterates through numbers from 2 to the square root of the given number to find its divisors.
- For each divisor found, it adds it to the sum, including the corresponding divisor on the other side of the square root.
- Finally, it compares the sum of divisors with the original number to determine if it is abundant.
π Between the Given Range
Let's take a look at the Python code that checks for abundant numbers in the specified range.
def get_proper_divisors_sum(number):
divisors_sum = 1 # Start with 1 as 1 is always a divisor
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
divisors_sum += i
if i != number // i:
divisors_sum += number // i
return divisors_sum
def is_abundant_number(number):
return get_proper_divisors_sum(number) > number
# Check for abundant numbers in the range 1 to 50
abundant_numbers = [num for num in range(1, 51) if is_abundant_number(num)]
# Print the abundant numbers
print("Abundant numbers in the range 1 to 50:", abundant_numbers)
π» Testing the Program
Abundant numbers in the range 1 to 50: [12, 18, 20, 24, 30, 36, 40, 42, 48]
Run the program to see the abundant numbers within the specified range.
π§ How the Program Works
- The program defines a function get_proper_divisors_sum to calculate the sum of proper divisors for a given number.
- The function is_abundant_number checks whether a number is abundant by comparing the sum of its proper divisors with the number itself.
- The main section utilizes list comprehension to generate a list of abundant numbers in the range of 1 to 50.
- The result is printed to the console.
π§ Understanding the Concept of Abundant Number
Before diving into the code, let's take a moment to understand abundant numbers. An abundant number is a positive integer for which the sum of its proper divisors (excluding itself) is greater than the number itself.
For example, the number 12 is abundant because its divisors (excluding 12) are 1, 2, 3, 4, 6, and the sum of these divisors is 16, which is greater than 12.
π’ Optimizing the Program
While the provided program is effective, there are opportunities for optimization, such as caching previously calculated divisor sums to avoid redundant computations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Abundant Number), please comment here. I will help you immediately.