Given two numbers ‘a’ and ‘b’ such that (0 <= a <= 10^12 and b <= b < 10^250). Find the GCD of two given numbers.

**Examples :**

Input: a = 978 b = 89798763754892653453379597352537489494736 Output: 6 Input: a = 1221 b = 1234567891011121314151617181920212223242526272829 Output: 3

__ Solution :__ In the given problem, we can see that first number ‘a’ can be handled by long long int data type but second number ‘b’ can not be handled by any int data type. Here we read second number as a string and we will try to make it less than and equal to ‘a’ by taking it’s modulo with ‘a’.

Below is implementation of the above idea.

## C++

`// C++ program to find GCD of two numbers such that ` `// the second number can be very large. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` `typedef` `long` `long` `int` `ll; ` ` ` `// function to find gcd of two integer numbers ` `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); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `// first number which is integer ` ` ` `ll a = 1221; ` ` ` ` ` `// second number is represented as string because ` ` ` `// it can not be handled by integer data type ` ` ` `char` `b[] = ` `"1234567891011121314151617181920212223242526272829"` `; ` ` ` `if` `(a == 0) ` ` ` `cout << b; ` ` ` `else` ` ` `cout << gcdLarge(a, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ` `// GCD of two numbers ` `// such that the second ` `// number can be very large. ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// This function computes ` ` ` `// the gcd of 2 numbers ` ` ` `private` `static` `int` `gcd(` `int` `reduceNum, ` `int` `b) ` ` ` `{ ` ` ` `return` `b == ` `0` `? ` ` ` `reduceNum : gcd(b, reduceNum % b); ` ` ` `} ` ` ` ` ` `// 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 ` ` ` `// modulus with first integer ` ` ` `// number using basic mathematics ` ` ` `private` `static` `int` `reduceB(` `int` `a, String b) ` ` ` `{ ` ` ` `int` `result = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < b.length(); i++) ` ` ` `{ ` ` ` `result = (result * ` `10` `+ ` ` ` `b.charAt(i) - ` `'0'` `) % a; ` ` ` `} ` ` ` `return` `result; ` ` ` `} ` ` ` ` ` `private` `static` `int` `gcdLarge(` `int` `a, String b) ` ` ` `{ ` ` ` `// Reduce 'b' i.e the second ` ` ` `// number after modulo with a ` ` ` `int` `num = reduceB(a, b); ` ` ` ` ` `// Now,use the euclid's algorithm ` ` ` `// to find the gcd of the 2 numbers ` ` ` `return` `gcd(num, a); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// First Number which ` ` ` `// is the integer ` ` ` `int` `a = ` `1221` `; ` ` ` ` ` `// Second Number is represented ` ` ` `// as a string because it cannot ` ` ` `// be represented as an integer ` ` ` `// data type ` ` ` `String b = ` `"19837658191095787329"` `; ` ` ` `if` `(a == ` `0` `) ` ` ` `System.out.println(b); ` ` ` `else` ` ` `System.out.println(gcdLarge(a, b)); ` ` ` `} ` ` ` `// This code is contributed ` `// by Tanishq Saluja. ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find GCD of ` `# two numbers such that the second ` `# number can be very large. ` ` ` ` ` `# Function to find gcd ` `# of two integer numbers ` `def` `gcd(a, b) : ` ` ` ` ` `if` `(a ` `=` `=` `0` `) : ` ` ` `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 ` `def` `reduceB(a, b) : ` ` ` ` ` `# Initialize result ` ` ` `mod ` `=` `0` ` ` ` ` `# Calculating mod of b with a ` ` ` `# to make b like 0 <= b < a ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `len` `(b)) : ` ` ` ` ` `mod ` `=` `(mod ` `*` `10` `+` `ord` `(b[i])) ` `%` `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 ` `def` `gcdLarge(a, b) : ` ` ` ` ` `# Reduce 'b' (second number) ` ` ` `# after modulo with a ` ` ` `num ` `=` `reduceB(a, b) ` ` ` ` ` `# gcd of two numbers ` ` ` `return` `gcd(a, num) ` ` ` ` ` `# Driver program ` ` ` `# First number which is integer ` `a ` `=` `1221` ` ` `# Second number is represented ` `# as string because it can not ` `# be handled by integer data type ` `b ` `=` `"1234567891011121314151617181920212223242526272829"` `if` `a ` `=` `=` `0` `: ` ` ` `print` `(b) ` `else` `: ` ` ` `print` `(gcdLarge(a, b)) ` ` ` ` ` `# This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find GCD of ` `// two numbers such that the ` `// second number can be very large. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// function to find gcd ` `// of two integer numbers ` `public` `long` `gcd(` `long` `a, ` `long` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `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 ` `public` `long` `reduceB(` `long` `a, ` `string` `b) ` `{ ` ` ` `// Initialize result ` ` ` `long` `mod = 0; ` ` ` ` ` `// calculating mod of ` ` ` `// b with a to make ` ` ` `// b like 0 <= b < a ` ` ` `for` `(` `int` `i = 0; i < b.Length; i++) ` ` ` `mod = (mod * 10 + ` ` ` `(b[i] - ` `'0'` `)) % a; ` ` ` ` ` `return` `mod; ` `} ` ` ` `// This function returns GCD ` `// of 'a' and 'b' where b can ` `// be very large and is ` `// represented as a character ` `// array or string ` `public` `long` `gcdLarge(` `long` `a, ` `string` `b) ` `{ ` ` ` `// Reduce 'b' (second number) ` ` ` `// after modulo with a ` ` ` `long` `num = reduceB(a, b); ` ` ` ` ` `// gcd of two numbers ` ` ` `return` `gcd(a, num); ` `} ` ` ` `// Driver Code ` `static` `void` `Main() ` `{ ` ` ` `// first number ` ` ` `// which is integer ` ` ` `long` `a = 1221; ` ` ` ` ` `// second number is represented ` ` ` `// as string because it can not ` ` ` `// be handled by integer data type ` ` ` `string` `b = ` `"1234567891011121314151617181920212223242526272829"` `; ` ` ` `GFG p = ` `new` `GFG(); ` ` ` `if` `(a == 0) ` ` ` `Console.WriteLine(b); ` ` ` `else` ` ` `Console.WriteLine(p.gcdLarge(a, b)); ` ` ` `} ` `} ` ` ` `// This code is contributed by mits. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find GCD of ` `// two numbers such that the ` `// second number can be very large. ` ` ` `// function to find gcd of ` `// two integer numbers ` `function` `gcd(` `$a` `, ` `$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 ` `function` `reduceB(` `$a` `, ` `$b` `) ` `{ ` ` ` `// Initialize result ` ` ` `$mod` `= 0; ` ` ` ` ` `// calculating mod of b with ` ` ` `// a to make b like 0 <= b < a ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `strlen` `(` `$b` `); ` `$i` `++) ` ` ` `$mod` `= (` `$mod` `* 10 + ` ` ` `$b` `[` `$i` `] - ` `'0'` `) % ` `$a` `; ` ` ` ` ` `// return modulo ` ` ` `return` `$mod` `; ` `} ` ` ` `// This function returns GCD of ` `// 'a' and 'b' where b can be ` `// very large and is represented ` `// as a character array or string ` `function` `gcdLarge(` `$a` `, ` `$b` `) ` `{ ` ` ` `// Reduce 'b' (second number) ` ` ` `// after modulo with a ` ` ` `$num` `= reduceB(` `$a` `, ` `$b` `); ` ` ` ` ` `// gcd of two numbers ` ` ` `return` `gcd(` `$a` `, ` `$num` `); ` `} ` ` ` `// Driver Code ` ` ` `// first number which is integer ` `$a` `= 1221; ` ` ` `// second number is represented ` `// as string because it can not ` `// be handled by integer data type ` `$b` `= ` `"1234567891011121314151617181920212223242526272829"` `; ` ` ` `if` `(` `$a` `== 0) { ` ` ` `echo` `(` `$b` `); ` `} ` `else` `{ ` ` ` `echo` `gcdLarge(` `$a` `, ` `$b` `); ` `} ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

3

This article is contributed by **Shashank Mishra (Gullu)**.This article is reviewed by team GeeksforGeeks . Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print all K digit repeating numbers in a very large number
- Find (a^b)%m where 'a' is very large
- Recursive sum of digit in n^x, where n and x are very large
- Check whether a very large number of the given form is a multiple of 3.
- Find (a^b)%m where 'b' is very large
- Series summation if T(n) is given and n is very large
- Comparing X^Y and Y^X for very large values of X and Y
- Number of factors of very large number N modulo M where M is any prime number
- How to store a very large number of more than 100 digits in C++
- Maximise the sum of two Numbers using at most one swap between them
- Multiply large integers under large modulo
- Find two numbers with sum N such that neither of them contains digit K
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Minimum number of integers required such that each Segment contains at least one of them
- Check if a large number can be divided into two or more segments of equal sum
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Sum of two large numbers
- Difference of two large numbers
- LCM of two large numbers
- Last digit of Product of two Large or Small numbers (a * b)