-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended Euclidean Algorithm in PHP (#2988)
* Extended Euclidean Algorithm in PHP * Update Extended_Euclidean_Algorithm.php * Update Extended_Euclidean_Algorithm.php
- Loading branch information
1 parent
52d9c24
commit a87620b
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* Extended Euclidean Algorithm | ||
============================== | ||
GCD of two numbers is the largest number that divides both of them. | ||
GCD(a,b) = ax + by | ||
If we can find the value of x and y then we can easily find the | ||
value of GCD(a,b) by replacing (x,y) with their respective values. | ||
*/ | ||
|
||
//Function to find GCD | ||
//This function returns an array having elements gcd, x and y | ||
function gcd($a, $b) | ||
{ | ||
// Base Case | ||
if ($a == 0) | ||
return array($b, 0, 1); | ||
|
||
// To store results of recursive call | ||
$arr = gcd($b % $a, $a); | ||
// Update x and y using results of recursive call | ||
$gcd = $arr[0]; | ||
$x = $arr[1]; | ||
$y = $arr[2]; | ||
return array($gcd, (int)($y - ($b / $a) * $x), $x); | ||
} | ||
|
||
// Driver Code | ||
$a = readline("Enter number 1 : "); | ||
$b = readline("Enter number 2 : "); | ||
$arr = gcd($a, $b); | ||
echo "\nGCD of ", $a, " and ", $b, " is ", $arr[0]; | ||
echo "\nx = ", $arr[1]; | ||
echo "\ny = ", $arr[2]; | ||
|
||
|
||
/* | ||
Sample input/output: | ||
Enter number 1 : 100 | ||
Enter number 2 : 20 | ||
GCD of 100 and 20 is 20 | ||
x = 0 | ||
y = 1 | ||
*/ | ||
?> |