Skip to content

Commit fc274c8

Browse files
authored
Fix style in AmicableNumbers (TheAlgorithms#4263)
1 parent e5c7a08 commit fc274c8

File tree

4 files changed

+91
-54
lines changed

4 files changed

+91
-54
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@
704704
* [NextFitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/NextFitTest.java)
705705
* [PasswordGenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/PasswordGenTest.java)
706706
* [SieveOfEratosthenesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SieveOfEratosthenesTest.java)
707+
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java)
707708
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
708709
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
709710
* [UniquePathsTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/UniquePathsTests.java)

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
<version>5.9.0</version>
4646
<scope>test</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.apache.commons</groupId>
50+
<artifactId>commons-lang3</artifactId>
51+
<version>3.12.0</version>
52+
</dependency>
4853
</dependencies>
4954

5055
<build>
Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,69 @@
11
package com.thealgorithms.maths;
22

3+
import java.util.LinkedHashSet;
4+
import java.util.Set;
5+
import org.apache.commons.lang3.tuple.Pair;
6+
37
/**
4-
* Amicable numbers are two different numbers so related that the sum of the
5-
* proper divisors of each is equal to the other number. (A proper divisor of a
6-
* number is a positive factor of that number other than the number itself. For
7-
* example, the proper divisors of 6 are 1, 2, and 3.) A pair of amicable
8-
* numbers constitutes an aliquot sequence of period 2. It is unknown if there
9-
* are infinitely many pairs of amicable numbers. *
8+
* Amicable numbers are two different natural numbers that the sum of the
9+
* proper divisors of each is equal to the other number.
10+
* (A proper divisor of a number is a positive factor of that number other than the number itself.
11+
* For example, the proper divisors of 6 are 1, 2, and 3.)
12+
* A pair of amicable numbers constitutes an aliquot sequence of period 2.
13+
* It is unknown if there are infinitely many pairs of amicable numbers.
1014
*
1115
* <p>
12-
* link: https://en.wikipedia.org/wiki/Amicable_numbers *
13-
*
16+
* link: https://en.wikipedia.org/wiki/Amicable_numbers
1417
* <p>
15-
* Simple Example : (220,284) 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110
16-
* } <- Sum = 284
17-
* 284 is divisible by -> 1,2,4,71,142 and the Sum of that is. Yes right you
18-
* probably expected it 220
18+
* Simple Example : (220, 284)
19+
* 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110} <- SUM = 284
20+
* 284 is divisible by {1,2,4,71,142} <- SUM = 220.
1921
*/
2022
public class AmicableNumber {
21-
22-
public static void main(String[] args) {
23-
AmicableNumber.findAllInRange(1, 3000);
24-
/* Res -> Int Range of 1 till 3000there are 3Amicable_numbers These are 1: = ( 220,284)
25-
2: = ( 1184,1210) 3: = ( 2620,2924) So it worked */
26-
}
27-
2823
/**
29-
* @param startValue
30-
* @param stopValue
31-
* @return
24+
* Finds all the amicable numbers in a given range.
25+
*
26+
* @param from range start value
27+
* @param to range end value (inclusive)
28+
* @return list with amicable numbers found in given range.
3229
*/
33-
static void findAllInRange(int startValue, int stopValue) {
34-
/* the 2 for loops are to avoid to double check tuple. For example (200,100) and (100,200)
35-
* is the same calculation also to avoid is to check the number with it self. a number with
36-
* itself is always a AmicableNumber
37-
* */
38-
StringBuilder res = new StringBuilder();
39-
int countofRes = 0;
30+
public static Set<Pair<Integer, Integer>> findAllInRange(int from, int to) {
31+
if (from <= 0 || to <= 0 || to < from) {
32+
throw new IllegalArgumentException("Given range of values is invalid!");
33+
}
34+
35+
Set<Pair<Integer, Integer>> result = new LinkedHashSet<>();
4036

41-
for (int i = startValue; i < stopValue; i++) {
42-
for (int j = i + 1; j <= stopValue; j++) {
37+
for (int i = from; i < to; i++) {
38+
for (int j = i + 1; j <= to; j++) {
4339
if (isAmicableNumber(i, j)) {
44-
countofRes++;
45-
res.append("" + countofRes + ": = ( " + i + "," + j + ")"
46-
+ "\t");
40+
result.add(Pair.of(i, j));
4741
}
4842
}
4943
}
50-
res.insert(0, "Int Range of " + startValue + " till " + stopValue + " there are " + countofRes + " Amicable_numbers.These are \n ");
51-
System.out.println(res);
44+
return result;
5245
}
5346

5447
/**
55-
* Check if {@code numberOne and numberTwo } are AmicableNumbers or not
56-
*
57-
* @param numberOne numberTwo
58-
* @return {@code true} if {@code numberOne numberTwo} isAmicableNumbers
59-
* otherwise false
48+
* Checks whether 2 numbers are AmicableNumbers or not.
6049
*/
61-
static boolean isAmicableNumber(int numberOne, int numberTwo) {
62-
return ((recursiveCalcOfDividerSum(numberOne, numberOne) == numberTwo && numberOne == recursiveCalcOfDividerSum(numberTwo, numberTwo)));
50+
public static boolean isAmicableNumber(int a, int b) {
51+
if (a <= 0 || b <= 0) {
52+
throw new IllegalArgumentException("Input numbers must be natural!");
53+
}
54+
return sumOfDividers(a, a) == b && sumOfDividers(b, b) == a;
6355
}
6456

6557
/**
66-
* calculated in recursive calls the Sum of all the Dividers beside it self
67-
*
68-
* @param number div = the next to test dividely by using the modulo
69-
* operator
70-
* @return sum of all the dividers
58+
* Recursively calculates the sum of all dividers for a given number excluding the divider itself.
7159
*/
72-
static int recursiveCalcOfDividerSum(int number, int div) {
73-
if (div == 1) {
60+
private static int sumOfDividers(int number, int divisor) {
61+
if (divisor == 1) {
7462
return 0;
75-
} else if (number % --div == 0) {
76-
return recursiveCalcOfDividerSum(number, div) + div;
63+
} else if (number % --divisor == 0) {
64+
return sumOfDividers(number, divisor) + divisor;
7765
} else {
78-
return recursiveCalcOfDividerSum(number, div);
66+
return sumOfDividers(number, divisor);
7967
}
8068
}
8169
}

src/test/java/com/thealgorithms/maths/AmicableNumberTest.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,57 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.util.Set;
6+
import org.apache.commons.lang3.tuple.Pair;
7+
import org.junit.jupiter.api.Assertions;
58
import org.junit.jupiter.api.Test;
69

710
public class AmicableNumberTest {
11+
private static final String INVALID_RANGE_EXCEPTION_MESSAGE = "Given range of values is invalid!";
12+
private static final String INVALID_NUMBERS_EXCEPTION_MESSAGE = "Input numbers must be natural!";
813

914
@Test
10-
void testAmicableNumber() {
15+
public void testShouldThrowExceptionWhenInvalidRangeProvided() {
16+
checkInvalidRange(0, 0);
17+
checkInvalidRange(0, 1);
18+
checkInvalidRange(1, 0);
19+
checkInvalidRange(10, -1);
20+
checkInvalidRange(-1, 10);
21+
}
22+
23+
@Test
24+
public void testShouldThrowExceptionWhenInvalidNumbersProvided() {
25+
checkInvalidNumbers(0, 0);
26+
checkInvalidNumbers(0, 1);
27+
checkInvalidNumbers(1, 0);
28+
}
29+
30+
@Test
31+
public void testAmicableNumbers() {
1132
assertThat(AmicableNumber.isAmicableNumber(220, 284)).isTrue();
1233
assertThat(AmicableNumber.isAmicableNumber(1184, 1210)).isTrue();
1334
assertThat(AmicableNumber.isAmicableNumber(2620, 2924)).isTrue();
1435
}
36+
37+
@Test
38+
public void testShouldFindAllAmicableNumbersInRange() {
39+
// given
40+
var expectedResult = Set.of(Pair.of(220, 284), Pair.of(1184, 1210), Pair.of(2620, 2924));
41+
42+
// when
43+
Set<Pair<Integer, Integer>> result = AmicableNumber.findAllInRange(1, 3000);
44+
45+
// then
46+
Assertions.assertTrue(result.containsAll(expectedResult));
47+
}
48+
49+
private static void checkInvalidRange(int from, int to) {
50+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> AmicableNumber.findAllInRange(from, to));
51+
Assertions.assertEquals(exception.getMessage(), INVALID_RANGE_EXCEPTION_MESSAGE);
52+
}
53+
54+
private static void checkInvalidNumbers(int a, int b) {
55+
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> AmicableNumber.isAmicableNumber(a, b));
56+
Assertions.assertEquals(exception.getMessage(), INVALID_NUMBERS_EXCEPTION_MESSAGE);
57+
}
1558
}

0 commit comments

Comments
 (0)