Fix style in AmicableNumbers (#4263)

This commit is contained in:
Albina Gimaletdinova
2023-07-26 16:52:46 +03:00
committed by GitHub
parent e5c7a08874
commit fc274c84f8
4 changed files with 91 additions and 54 deletions

View File

@ -704,6 +704,7 @@
* [NextFitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/NextFitTest.java) * [NextFitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/NextFitTest.java)
* [PasswordGenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/PasswordGenTest.java) * [PasswordGenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/PasswordGenTest.java)
* [SieveOfEratosthenesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SieveOfEratosthenesTest.java) * [SieveOfEratosthenesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/SieveOfEratosthenesTest.java)
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/StackPostfixNotationTest.java)
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java) * [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java) * [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
* [UniquePathsTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/UniquePathsTests.java) * [UniquePathsTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/UniquePathsTests.java)

View File

@ -45,6 +45,11 @@
<version>5.9.0</version> <version>5.9.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,81 +1,69 @@
package com.thealgorithms.maths; package com.thealgorithms.maths;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
/** /**
* Amicable numbers are two different numbers so related that the sum of the * Amicable numbers are two different natural numbers that the sum of the
* proper divisors of each is equal to the other number. (A proper divisor of a * proper divisors of each is equal to the other number.
* number is a positive factor of that number other than the number itself. For * (A proper divisor of a number is a positive factor of that number other than the number itself.
* example, the proper divisors of 6 are 1, 2, and 3.) A pair of amicable * For example, the proper divisors of 6 are 1, 2, and 3.)
* numbers constitutes an aliquot sequence of period 2. It is unknown if there * A pair of amicable numbers constitutes an aliquot sequence of period 2.
* are infinitely many pairs of amicable numbers. * * It is unknown if there are infinitely many pairs of amicable numbers.
* *
* <p> * <p>
* link: https://en.wikipedia.org/wiki/Amicable_numbers * * link: https://en.wikipedia.org/wiki/Amicable_numbers
*
* <p> * <p>
* Simple Example : (220,284) 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110 * Simple Example : (220, 284)
* } <- Sum = 284 * 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110} <- SUM = 284
* 284 is divisible by -> 1,2,4,71,142 and the Sum of that is. Yes right you * 284 is divisible by {1,2,4,71,142} <- SUM = 220.
* probably expected it 220
*/ */
public class AmicableNumber { public class AmicableNumber {
/**
public static void main(String[] args) { * Finds all the amicable numbers in a given range.
AmicableNumber.findAllInRange(1, 3000); *
/* Res -> Int Range of 1 till 3000there are 3Amicable_numbers These are 1: = ( 220,284) * @param from range start value
2: = ( 1184,1210) 3: = ( 2620,2924) So it worked */ * @param to range end value (inclusive)
* @return list with amicable numbers found in given range.
*/
public static Set<Pair<Integer, Integer>> findAllInRange(int from, int to) {
if (from <= 0 || to <= 0 || to < from) {
throw new IllegalArgumentException("Given range of values is invalid!");
} }
/** Set<Pair<Integer, Integer>> result = new LinkedHashSet<>();
* @param startValue
* @param stopValue
* @return
*/
static void findAllInRange(int startValue, int stopValue) {
/* the 2 for loops are to avoid to double check tuple. For example (200,100) and (100,200)
* is the same calculation also to avoid is to check the number with it self. a number with
* itself is always a AmicableNumber
* */
StringBuilder res = new StringBuilder();
int countofRes = 0;
for (int i = startValue; i < stopValue; i++) { for (int i = from; i < to; i++) {
for (int j = i + 1; j <= stopValue; j++) { for (int j = i + 1; j <= to; j++) {
if (isAmicableNumber(i, j)) { if (isAmicableNumber(i, j)) {
countofRes++; result.add(Pair.of(i, j));
res.append("" + countofRes + ": = ( " + i + "," + j + ")"
+ "\t");
} }
} }
} }
res.insert(0, "Int Range of " + startValue + " till " + stopValue + " there are " + countofRes + " Amicable_numbers.These are \n "); return result;
System.out.println(res);
} }
/** /**
* Check if {@code numberOne and numberTwo } are AmicableNumbers or not * Checks whether 2 numbers are AmicableNumbers or not.
*
* @param numberOne numberTwo
* @return {@code true} if {@code numberOne numberTwo} isAmicableNumbers
* otherwise false
*/ */
static boolean isAmicableNumber(int numberOne, int numberTwo) { public static boolean isAmicableNumber(int a, int b) {
return ((recursiveCalcOfDividerSum(numberOne, numberOne) == numberTwo && numberOne == recursiveCalcOfDividerSum(numberTwo, numberTwo))); if (a <= 0 || b <= 0) {
throw new IllegalArgumentException("Input numbers must be natural!");
}
return sumOfDividers(a, a) == b && sumOfDividers(b, b) == a;
} }
/** /**
* calculated in recursive calls the Sum of all the Dividers beside it self * Recursively calculates the sum of all dividers for a given number excluding the divider itself.
*
* @param number div = the next to test dividely by using the modulo
* operator
* @return sum of all the dividers
*/ */
static int recursiveCalcOfDividerSum(int number, int div) { private static int sumOfDividers(int number, int divisor) {
if (div == 1) { if (divisor == 1) {
return 0; return 0;
} else if (number % --div == 0) { } else if (number % --divisor == 0) {
return recursiveCalcOfDividerSum(number, div) + div; return sumOfDividers(number, divisor) + divisor;
} else { } else {
return recursiveCalcOfDividerSum(number, div); return sumOfDividers(number, divisor);
} }
} }
} }

View File

@ -2,14 +2,57 @@ package com.thealgorithms.maths;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.util.Set;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class AmicableNumberTest { public class AmicableNumberTest {
private static final String INVALID_RANGE_EXCEPTION_MESSAGE = "Given range of values is invalid!";
private static final String INVALID_NUMBERS_EXCEPTION_MESSAGE = "Input numbers must be natural!";
@Test @Test
void testAmicableNumber() { public void testShouldThrowExceptionWhenInvalidRangeProvided() {
checkInvalidRange(0, 0);
checkInvalidRange(0, 1);
checkInvalidRange(1, 0);
checkInvalidRange(10, -1);
checkInvalidRange(-1, 10);
}
@Test
public void testShouldThrowExceptionWhenInvalidNumbersProvided() {
checkInvalidNumbers(0, 0);
checkInvalidNumbers(0, 1);
checkInvalidNumbers(1, 0);
}
@Test
public void testAmicableNumbers() {
assertThat(AmicableNumber.isAmicableNumber(220, 284)).isTrue(); assertThat(AmicableNumber.isAmicableNumber(220, 284)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(1184, 1210)).isTrue(); assertThat(AmicableNumber.isAmicableNumber(1184, 1210)).isTrue();
assertThat(AmicableNumber.isAmicableNumber(2620, 2924)).isTrue(); assertThat(AmicableNumber.isAmicableNumber(2620, 2924)).isTrue();
} }
@Test
public void testShouldFindAllAmicableNumbersInRange() {
// given
var expectedResult = Set.of(Pair.of(220, 284), Pair.of(1184, 1210), Pair.of(2620, 2924));
// when
Set<Pair<Integer, Integer>> result = AmicableNumber.findAllInRange(1, 3000);
// then
Assertions.assertTrue(result.containsAll(expectedResult));
}
private static void checkInvalidRange(int from, int to) {
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> AmicableNumber.findAllInRange(from, to));
Assertions.assertEquals(exception.getMessage(), INVALID_RANGE_EXCEPTION_MESSAGE);
}
private static void checkInvalidNumbers(int a, int b) {
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> AmicableNumber.isAmicableNumber(a, b));
Assertions.assertEquals(exception.getMessage(), INVALID_NUMBERS_EXCEPTION_MESSAGE);
}
} }