-
Notifications
You must be signed in to change notification settings - Fork 5
/
GCD2.cpp
42 lines (34 loc) · 981 Bytes
/
GCD2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//GCD of two numbers when one number can be very large
/******SupreethBaliga******/
//ll -> long long
ll gcd(ll a, ll b)
{
if (!a)
return b;
return gcd(b%a,a);
}
// Here 'a' is integer and 'b' is string.
// The idea is to make the second number (represented
// as b) less than and equal to first number by
// calculating its mod with first integer number
// using basic mathematics
ll reduceB(ll a, char b[])
{
// Initialize result
ll mod = 0;
// calculating mod of b with a to make
// b like 0 <= b < a
for (int i=0; i<strlen(b); i++)
mod = (mod*10 + b[i] - '0')%a;
return mod; // return modulo
}
// This function returns GCD of 'a' and 'b'
// where b can be very large and is represented
// as a character array or string
ll gcdLarge(ll a, char b[])
{
// Reduce 'b' (second number) after modulo with a
ll num = reduceB(a, b);
// gcd of two numbers
return gcd(a, num);
}