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 Algo in Dart #2977

Merged
merged 1 commit into from
May 30, 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
42 changes: 42 additions & 0 deletions Extended_Euclidean_Algorithm/Extended_Euclidean_Algorithm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Extended Euclidean Algorithm
==============================
GCD of two numbers is the largest number that divides both of them.
A simple way to find GCD is to factorize both numbers and multiply
common factors.

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

import 'dart:io';

void main() {
int x = 0, y = 0;
var a = int.parse(stdin.readLineSync());
var b = int.parse(stdin.readLineSync());
// function called for 98 and 21
print(gcdFunction(a, b, x, y));
}

int gcdFunction(a, b, x, y) {
if (a == 0) {
x = 0;
y = 0;
return b;
}

int x1 = 0, y1 = 0;
int gcd = gcdFunction(b % a, a, x1, y1);

x = y1 - (b / a).round() * x1;
y = x1;

return gcd;
}

// Sample input :
Copy link
Contributor

Choose a reason for hiding this comment

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

Use /* */instead of // for writing multi-line comment.

// 98
// 21
// Sample output :
// 7