Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended Euclidean Algorithm in PHP #2988

Merged
merged 3 commits into from
May 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.php
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a empty line after <?php

//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
*/
?>