Last Updated on May 15, 2023 by Prepbytes
This article discusses a program that determines if a given number is abundant or not. A number is said to be plentiful if the total of its appropriate divisors exceeds the number itself.
What is an Abundant Number in Java?
If the sum of all its proper divisors, denoted by sum(n), is greater than the number’s value, the number is said to be abundant. The difference between these two values is referred to as abundance. If the following condition is met, the number is said to be an abundant number:
- sum(n)> n
- abundance = sum(n) – n
- sum(n): aliquot sum – the sum of all n’s proper divisors.
Check whether the Number is an Abundant Number or Not in Java
Given an integer input, the goal is to determine whether the sum of all the number’s factors, excluding the number itself, is greater than the number. To be an abundant number, any number must satisfy the following conditions:
- Except for itself, the sum of its factors must be greater than the number.
- As a result, we will first find all the factors of the number that do not include the number itself within the range 1 to number/2.
- Meanwhile, we’ll keep adding up the factors and compare them to the final figure.
- If the number itself is included as a factor, the sum must be divided by 2 before comparing.
We will look at two approaches to resolving this problem.
Method 1: Using Java’s Range until Number
- Begin the program.
- Declare and initialize two integer variables, ‘n’, and sum with values 12 and 0.
- Use a for loop with variable ‘i’ that starts at 1 and ends at ‘n-1’.
- Check to see if ‘n’ is divisible by ‘i’, that is, n% i == 0’.
- If ‘n’ is divisible by ‘i’, add ‘i’ to sum, i.e.’sum = sum + i’.
- Determine whether the sum is greater than ‘n’.
- If the sum is greater than the number n, print "n is an Abundant Number."
- Subtract ‘n’ from sum’ to find the abundance of the number.
- "The Abundance is:" followed by the calculated abundance value.
- If Sum is less than or equal to ‘n,’ display "n is not an Abundant Number."
- Finish the program.
Code Implementation
public class Main { public static void main (String[]args) { int n = 12, sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) sum = sum + i; } if (sum > n) { System.out.println(n + " is an Abundant Number"); System.out.println("The Abundance is: " + (sum - n)); } else System.out.println(n + " is not an Abundant Number"); } }
Output:
12 is an Abundant Number
The Abundance is: 6
Method 2: Using Java’s Range until Sqrt
- Begin by declaring and initializing the variable n to a suitable integer value.
- Declare the variable sum and assign it the outcome of calling the getSum function with the argument n.
- Check to see if the total is greater than n.
- If the sum exceeds n, a message indicating that n is an abundant number and the abundance value is printed.
- If the sum is less than n, a message indicating that n is not an abundant number is printed.
- Declare the function getSum with an integer parameter n.
- Declare and set a variable sum to 0.
- Iterate from 1 to the square root of n using a for loop.
- Check whether n is divisible by i within the loop.
- If n is divisible by i, find the two divisors that form the pair with i and add them to the sum.
- If i equals 1, then only i should be added to the sum.
- If i equals the square root of n, then only i should be added to the sum.
- The function’s sum value is returned.
- The program has come to an end.
Code Implementation
public class Main { public static void main(String[] args) { int n = 12; int sum = getSum(n); if (sum > n) { System.out.println(n + " is an Abundant Number"); } else { System.out.println(n + " is not an Abundant Number"); } } static int getSum(int n) { int sum = 0; for (int i = 1; i < Math.sqrt(n); i++) { if (n % i == 0) { if (i == 1) sum = sum + i; else if (i == n / i) sum = sum + i; else sum = sum + i + n / i; } } return sum; } }
Output
12 is an Abundant Number
Conclusion
Finally, this article describes the idea of abundant numbers and includes two Java methods for determining if a given number is abundant or not. Method 1 used a for loop to run through the range 1 to n-1, determining if n is divisible by i and adding i to the sum. Method 2 uses a function to compute the sum of all components until the square root of n is reached. For the provided input of 12, which is an abundant number with an abundance of 6, both approaches generate the same outcome. These methods can be used for any integer input to determine whether or not it is an abundant number.
Frequently Asked Questions (FAQs)
Q1. Is 32 abundant or deficient?
Ans. 32 is not an abundant number. Its proper divisors are 1, 2, 4, 8, and 16, and their sum is 31 (less than 32). As a result, 32 is the least sum of two distinct abundant numbers that are not abundant.
Q2. What are abundant, deficient, and perfect numbers in Java?
Ans. If the sum of the factors excluding itself equals that number, it is perfect; if it exceeds that number, it is abundant; and if it falls below that number, it is deficient.
Q3. Which numbers are not abundant?
Ans. Non-abundant numbers are classified into two types: perfect numbers and deficient numbers. A perfect number is one in which the sum of its proper divisors equals the number itself. A deficient number is one in which the sum of its proper divisors is less than the number itself.