-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
asha15
merged 3 commits into
jainaman224:master
from
utkarshanehe:Extended_Euclidean_Algorithm
May 29, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
*/ | ||
?> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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