Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89c7c04

Browse files
commit1
1 parentb76962d commit89c7c04

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
packagecom.thealgorithms.maths;
2+
3+
/**
4+
* In mathematics, the extended Euclidean algorithm is an extension to the
5+
* Euclidean algorithm, and computes, in addition to the greatest common divisor
6+
* (gcd) of integers a and b, also the coefficients of Bézout's identity, which
7+
* are integers x and y such that ax + by = gcd(a, b).
8+
*
9+
* <p>
10+
* For more details, see
11+
* https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
12+
*/
13+
publicfinalclassExtendedEuclideanAlgorithm {
14+
15+
privateExtendedEuclideanAlgorithm() {
16+
}
17+
18+
/**
19+
* This method implements the extended Euclidean algorithm.
20+
*
21+
* @param a The first number.
22+
* @param b The second number.
23+
* @return An array of three integers:
24+
* <ul>
25+
* <li>Index 0: The greatest common divisor (gcd) of a and b.</li>
26+
* <li>Index 1: The value of x in the equation ax + by = gcd(a, b).</li>
27+
* <li>Index 2: The value of y in the equation ax + by = gcd(a, b).</li>
28+
* </ul>
29+
*/
30+
publicstaticlong[]extendedGCD(longa,longb) {
31+
if (b ==0) {
32+
// Base case: gcd(a, 0) = a. The equation is a*1 + 0*0 = a.
33+
returnnewlong[] {a,1,0 };
34+
}
35+
36+
// Recursive call
37+
long[]result =extendedGCD(b,a %b);
38+
longgcd =result[0];
39+
longx1 =result[1];
40+
longy1 =result[2];
41+
42+
// Update coefficients using the results from the recursive call
43+
longx =y1;
44+
longy =x1 -a /b *y1;
45+
46+
returnnewlong[] {gcd,x,y };
47+
}
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.thealgorithms.maths;
2+
3+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
4+
5+
importorg.junit.jupiter.api.Test;
6+
7+
publicclassExtendedEuclideanAlgorithmTest {
8+
9+
/**
10+
* Verifies that the returned values satisfy Bézout's identity: a*x + b*y =
11+
* gcd(a, b)
12+
*/
13+
privatevoidverifyBezoutIdentity(longa,longb,long[]result) {
14+
longgcd =result[0];
15+
longx =result[1];
16+
longy =result[2];
17+
assertEquals(a *x +b *y,gcd,"Bézout's identity failed for gcd(" +a +", " +b +")");
18+
}
19+
20+
@Test
21+
publicvoidtestExtendedGCD() {
22+
// Test case 1: General case gcd(30, 50) = 10
23+
long[]result1 =ExtendedEuclideanAlgorithm.extendedGCD(30,50);
24+
assertEquals(10,result1[0],"Test Case 1 Failed: gcd(30, 50) should be 10");
25+
verifyBezoutIdentity(30,50,result1);
26+
27+
// Test case 2: Another general case gcd(240, 46) = 2
28+
long[]result2 =ExtendedEuclideanAlgorithm.extendedGCD(240,46);
29+
assertEquals(2,result2[0],"Test Case 2 Failed: gcd(240, 46) should be 2");
30+
verifyBezoutIdentity(240,46,result2);
31+
32+
// Test case 3: Base case where b is 0, gcd(10, 0) = 10
33+
long[]result3 =ExtendedEuclideanAlgorithm.extendedGCD(10,0);
34+
assertEquals(10,result3[0],"Test Case 3 Failed: gcd(10, 0) should be 10");
35+
verifyBezoutIdentity(10,0,result3);
36+
37+
// Test case 4: Numbers are co-prime gcd(17, 13) = 1
38+
long[]result4 =ExtendedEuclideanAlgorithm.extendedGCD(17,13);
39+
assertEquals(1,result4[0],"Test Case 4 Failed: gcd(17, 13) should be 1");
40+
verifyBezoutIdentity(17,13,result4);
41+
42+
// Test case 5: One number is a multiple of the other gcd(100, 20) = 20
43+
long[]result5 =ExtendedEuclideanAlgorithm.extendedGCD(100,20);
44+
assertEquals(20,result5[0],"Test Case 5 Failed: gcd(100, 20) should be 20");
45+
verifyBezoutIdentity(100,20,result5);
46+
}
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp