Skip to content

Commit 580ed9f

Browse files
antmordelmaibin
authored andcommitted
BAEL 2772 - Find If 2 Numbers Are Relatively Prime in Java (eugenp#6599)
* Hexagonal Architecture - Eval Article * BAEL-2772 Relatively Prime code
1 parent 7ac699f commit 580ed9f

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.algorithms.relativelyprime;
2+
3+
import java.math.BigInteger;
4+
5+
class RelativelyPrime {
6+
7+
static boolean iterativeRelativelyPrime(int a, int b) {
8+
return iterativeGCD(a, b) == 1;
9+
}
10+
11+
static boolean recursiveRelativelyPrime(int a, int b) {
12+
return recursiveGCD(a, b) == 1;
13+
}
14+
15+
static boolean bigIntegerRelativelyPrime(int a, int b) {
16+
return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).equals(BigInteger.ONE);
17+
}
18+
19+
private static int iterativeGCD(int a, int b) {
20+
int tmp;
21+
while (b != 0) {
22+
if (a < b) {
23+
tmp = a;
24+
a = b;
25+
b = tmp;
26+
}
27+
tmp = b;
28+
b = a % b;
29+
a = tmp;
30+
}
31+
return a;
32+
}
33+
34+
private static int recursiveGCD(int a, int b) {
35+
if (b == 0) {
36+
return a;
37+
}
38+
if (a < b) {
39+
return recursiveGCD(b, a);
40+
}
41+
return recursiveGCD(b, a % b);
42+
}
43+
44+
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.algorithms.relativelyprime;
2+
3+
import org.junit.Test;
4+
5+
import static com.baeldung.algorithms.relativelyprime.RelativelyPrime.*;
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
public class RelativelyPrimeUnitTest {
9+
10+
@Test
11+
public void givenNonRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnFalse() {
12+
13+
boolean result = iterativeRelativelyPrime(45, 35);
14+
assertThat(result).isFalse();
15+
}
16+
17+
@Test
18+
public void givenRelativelyPrimeNumbers_whenCheckingIteratively_shouldReturnTrue() {
19+
20+
boolean result = iterativeRelativelyPrime(500, 501);
21+
assertThat(result).isTrue();
22+
}
23+
24+
@Test
25+
public void givenNonRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnFalse() {
26+
27+
boolean result = recursiveRelativelyPrime(45, 35);
28+
assertThat(result).isFalse();
29+
}
30+
31+
@Test
32+
public void givenRelativelyPrimeNumbers_whenCheckingRecursively_shouldReturnTrue() {
33+
34+
boolean result = recursiveRelativelyPrime(500, 501);
35+
assertThat(result).isTrue();
36+
}
37+
38+
@Test
39+
public void givenNonRelativelyPrimeNumbers_whenCheckingUsingBigIntegers_shouldReturnFalse() {
40+
41+
boolean result = bigIntegerRelativelyPrime(45, 35);
42+
assertThat(result).isFalse();
43+
}
44+
45+
@Test
46+
public void givenRelativelyPrimeNumbers_whenCheckingBigIntegers_shouldReturnTrue() {
47+
48+
boolean result = bigIntegerRelativelyPrime(500, 501);
49+
assertThat(result).isTrue();
50+
}
51+
}

0 commit comments

Comments
 (0)