Skip to content

Commit 25fb744

Browse files
added program for abundant number
1 parent a4cf6e3 commit 25fb744

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* In number theory, an abundant number or excessive number is a positive integer for which
5+
* the sum of its proper divisors is greater than the number.
6+
* Equivalently, it is a number for which the sum of proper divisors (or aliquot sum) is greater than n.
7+
*
8+
* The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16.
9+
*
10+
* Wiki: https://en.wikipedia.org/wiki/Abundant_number
11+
*/
12+
public final class AbundantNumber {
13+
14+
private AbundantNumber() {
15+
}
16+
17+
// Function to calculate sum of all divisors including n
18+
private static int sumOfDivisors(int n) {
19+
int sum = 1 + n; // 1 and n are always divisors
20+
for (int i = 2; i <= n / 2; i++) {
21+
if (n % i == 0) sum += i; // adding divisor to sum
22+
}
23+
return sum;
24+
}
25+
26+
// Common validation method
27+
private static void validatePositiveNumber(int number) {
28+
if (number <= 0) {
29+
throw new IllegalArgumentException("Number must be positive.");
30+
}
31+
}
32+
33+
/**
34+
* Check if {@code number} is an Abundant number or not by checking sum of divisors > 2n
35+
*
36+
* @param number the number
37+
* @return {@code true} if {@code number} is an Abundant number, otherwise false
38+
*/
39+
public static boolean isAbundant(int number) {
40+
validatePositiveNumber(number);
41+
42+
return sumOfDivisors(number) > 2 * number;
43+
}
44+
45+
/**
46+
* Check if {@code number} is an Abundant number or not by checking Aliquot Sum > n
47+
*
48+
* @param number the number
49+
* @return {@code true} if {@code number} is a Abundant number, otherwise false
50+
*/
51+
public static boolean isAbundantNumber(int number) {
52+
validatePositiveNumber(number);
53+
54+
return AliquotSum.getAliquotSum(number) > number;
55+
}
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.CsvSource;
9+
10+
class AbundantNumberTest {
11+
@ParameterizedTest
12+
@CsvSource({"12", "66", "222", "444", "888", "2424"})
13+
void smithNumbersTest(int n) {
14+
assertTrue(AbundantNumber.isAbundant(n));
15+
assertTrue(AbundantNumber.isAbundantNumber(n));
16+
}
17+
18+
@ParameterizedTest
19+
@CsvSource({"1", "2", "6", "111", "333", "2222"})
20+
void odiousNumbersTest(int n) {
21+
assertFalse(AbundantNumber.isAbundant(n));
22+
assertFalse(AbundantNumber.isAbundantNumber(n));
23+
}
24+
25+
@ParameterizedTest
26+
@CsvSource({"0", "-1"})
27+
void throwsNegativeNumbersNotAllowed(int n) {
28+
assertThrows(IllegalArgumentException.class, () -> AbundantNumber.isAbundant(n));
29+
assertThrows(IllegalArgumentException.class, () -> AbundantNumber.isAbundantNumber(n));
30+
}
31+
}

0 commit comments

Comments
 (0)