Skip to content

Commit 1aee4d6

Browse files
authored
Merge branch 'master' into master-3
2 parents 39b32a2 + fc274c8 commit 1aee4d6

File tree

8 files changed

+222
-134
lines changed

8 files changed

+222
-134
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@
608608
* [EggDroppingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/EggDroppingTest.java)
609609
* [KnapsackMemoizationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackMemoizationTest.java)
610610
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
611+
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
611612
* [OptimalJobSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/OptimalJobSchedulingTest.java)
612613
* [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java)
613614
* [SubsetCountTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java)
@@ -637,6 +638,7 @@
637638
* [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java)
638639
* [FastInverseSqrtTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java)
639640
* [FFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FFTTest.java)
641+
* [FibonacciJavaStreamsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciJavaStreamsTest.java)
640642
* [FibonacciNumberCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciNumberCheckTest.java)
641643
* [FindMaxTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMaxTest.java)
642644
* [FindMinTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMinTest.java)
@@ -673,6 +675,7 @@
673675
* [SquareRootWithNewtonRaphsonTestMethod](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java)
674676
* [StandardDeviationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/StandardDeviationTest.java)
675677
* [StandardScoreTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/StandardScoreTest.java)
678+
* [SumOfArithmeticSeriesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumOfArithmeticSeriesTest.java)
676679
* [SumOfDigitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumOfDigitsTest.java)
677680
* [SumWithoutArithmeticOperatorsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java)
678681
* [TestArmstrong](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TestArmstrong.java)
@@ -701,6 +704,7 @@
701704
* [NextFitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/NextFitTest.java)
702705
* [PasswordGenTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/PasswordGenTest.java)
703706
* [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)
704708
* [TestPrintMatrixInSpiralOrder](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TestPrintMatrixInSpiralOrder.java)
705709
* [TwoPointersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/TwoPointersTest.java)
706710
* [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/main/java/com/thealgorithms/maths/FibonacciJavaStreams.java

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.math.BigDecimal;
44
import java.util.List;
5-
import java.util.Objects;
65
import java.util.Optional;
76
import java.util.stream.Stream;
87

@@ -14,7 +13,7 @@ public class FibonacciJavaStreams {
1413

1514
public static Optional<BigDecimal> calculate(final BigDecimal index) {
1615
if (index == null || index.compareTo(BigDecimal.ZERO) < 0) {
17-
return Optional.empty();
16+
throw new IllegalArgumentException("Input index cannot be null or negative!");
1817
}
1918

2019
if (index.compareTo(BigDecimal.ONE) < 0) {
@@ -30,72 +29,4 @@ public static Optional<BigDecimal> calculate(final BigDecimal index) {
3029

3130
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(results.size() - 1));
3231
}
33-
34-
public static void assertThat(final Object actual, final Object expected) {
35-
if (!Objects.equals(actual, expected)) {
36-
throw new AssertionError(String.format("expected=%s but was actual=%s", expected, actual));
37-
}
38-
}
39-
40-
public static void main(final String[] args) {
41-
{
42-
final Optional<BigDecimal> result = calculate(new BigDecimal(-1));
43-
assertThat(result.isEmpty(), true);
44-
}
45-
{
46-
final Optional<BigDecimal> result = calculate(BigDecimal.ZERO);
47-
assertThat(result.isPresent(), true);
48-
result.ifPresent(value -> assertThat(value, BigDecimal.ZERO));
49-
}
50-
{
51-
final Optional<BigDecimal> result = calculate(BigDecimal.ONE);
52-
assertThat(result.isPresent(), true);
53-
result.ifPresent(value -> assertThat(value, BigDecimal.ONE));
54-
}
55-
{
56-
final Optional<BigDecimal> result = calculate(new BigDecimal(2));
57-
assertThat(result.isPresent(), true);
58-
result.ifPresent(value -> assertThat(value, BigDecimal.ONE));
59-
}
60-
{
61-
final Optional<BigDecimal> result = calculate(new BigDecimal(3));
62-
assertThat(result.isPresent(), true);
63-
result.ifPresent(value -> assertThat(value, new BigDecimal(2)));
64-
}
65-
{
66-
final Optional<BigDecimal> result = calculate(new BigDecimal(10));
67-
assertThat(result.isPresent(), true);
68-
result.ifPresent(value -> assertThat(value, new BigDecimal(55)));
69-
}
70-
{
71-
final Optional<BigDecimal> result = calculate(new BigDecimal(20));
72-
assertThat(result.isPresent(), true);
73-
result.ifPresent(value -> assertThat(value, new BigDecimal(6765)));
74-
}
75-
{
76-
final Optional<BigDecimal> result = calculate(new BigDecimal(30));
77-
assertThat(result.isPresent(), true);
78-
result.ifPresent(value -> assertThat(value, new BigDecimal(832040)));
79-
}
80-
{
81-
final Optional<BigDecimal> result = calculate(new BigDecimal(40));
82-
assertThat(result.isPresent(), true);
83-
result.ifPresent(value -> assertThat(value, new BigDecimal(102334155)));
84-
}
85-
{
86-
final Optional<BigDecimal> result = calculate(new BigDecimal(50));
87-
assertThat(result.isPresent(), true);
88-
result.ifPresent(value -> assertThat(value, new BigDecimal(12586269025L)));
89-
}
90-
{
91-
final Optional<BigDecimal> result = calculate(new BigDecimal(100));
92-
assertThat(result.isPresent(), true);
93-
result.ifPresent(value -> assertThat(value, new BigDecimal("354224848179261915075")));
94-
}
95-
{
96-
final Optional<BigDecimal> result = calculate(new BigDecimal(200));
97-
assertThat(result.isPresent(), true);
98-
result.ifPresent(value -> assertThat(value, new BigDecimal("280571172992510140037611932413038677189525")));
99-
}
100-
}
10132
}
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package com.thealgorithms.others;
22

3-
import java.util.*;
3+
import java.util.Scanner;
4+
import java.util.Stack;
45

5-
public class StackPostfixNotation {
6-
7-
public static void main(String[] args) {
8-
Scanner scanner = new Scanner(System.in);
9-
String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"
10-
System.out.println(postfixEvaluate(post));
11-
scanner.close();
6+
public final class StackPostfixNotation {
7+
private StackPostfixNotation() {
128
}
139

1410
// Evaluates the given postfix expression string and returns the result.
15-
public static int postfixEvaluate(String exp) {
11+
public static int postfixEvaluate(final String exp) {
1612
Stack<Integer> s = new Stack<Integer>();
1713
Scanner tokens = new Scanner(exp);
1814

1915
while (tokens.hasNext()) {
2016
if (tokens.hasNextInt()) {
2117
s.push(tokens.nextInt()); // If int then push to stack
2218
} else { // else pop top two values and perform the operation
19+
if (s.size() < 2) {
20+
throw new IllegalArgumentException("exp is not a proper postfix expression (too few arguments).");
21+
}
2322
int num2 = s.pop();
2423
int num1 = s.pop();
2524
String op = tokens.next();
@@ -28,12 +27,16 @@ public static int postfixEvaluate(String exp) {
2827
case "+" -> s.push(num1 + num2);
2928
case "-" -> s.push(num1 - num2);
3029
case "*" -> s.push(num1 * num2);
31-
default -> s.push(num1 / num2);
30+
case "/" -> s.push(num1 / num2);
31+
default -> throw new IllegalArgumentException("exp contains an unknown operation.");
3232
}
3333
// "+", "-", "*", "/"
3434
}
3535
}
3636
tokens.close();
37+
if (s.size() != 1) {
38+
throw new IllegalArgumentException("exp is not a proper postfix expression.");
39+
}
3740
return s.pop();
3841
}
3942
}

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)