File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+ typedef long long int ll;
4
+
5
+ // function to find gcd of two integer numbers
6
+ ll gcd (ll a, ll b)
7
+ {
8
+ if (!a)
9
+ return b;
10
+ return gcd (b%a,a);
11
+ }
12
+
13
+ // Here 'a' is integer and 'b' is string.
14
+ // The idea is to make the second number (represented
15
+ // as b) less than and equal to first number by
16
+ // calculating its mod with first integer number
17
+ // using basic mathematics
18
+ ll reduceB (ll a, char b[])
19
+ {
20
+ // Initialize result
21
+ ll mod = 0 ;
22
+
23
+ // calculating mod of b with a to make
24
+ // b like 0 <= b < a
25
+ for (int i=0 ; i<strlen (b); i++)
26
+ mod = (mod*10 + b[i] - ' 0' )%a;
27
+
28
+ return mod; // return modulo
29
+ }
30
+
31
+ // This function returns GCD of 'a' and 'b'
32
+ // where b can be very large and is represented
33
+ // as a character array or string
34
+ ll gcdLarge (ll a, char b[])
35
+ {
36
+ // Reduce 'b' (second number) after modulo with a
37
+ ll num = reduceB (a, b);
38
+
39
+ // gcd of two numbers
40
+ return gcd (a, num);
41
+ }
42
+
43
+ // Driver program
44
+ int main ()
45
+ {
46
+ // first number which is integer
47
+ ll a = 1221 ;
48
+
49
+ // second number is represented as string because
50
+ // it can not be handled by integer data type
51
+ char b[] = " 1234567891011121314151617181920212223242526272829" ;
52
+
53
+ cout << gcdLarge (a, b);
54
+
55
+ return 0 ;
56
+ }
You can’t perform that action at this time.
0 commit comments