Given two numbers ‘a’ and ‘b’ such that (0 <= a <= 10^12 and b <= b < 10^250). Find the GCD of two given numbers.
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.
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.
- LCM of two large numbers
- Sum of two large numbers
- Difference of two large numbers
- Remainder with 7 for large numbers
- Divisible by 37 for large numbers
- Sum of first N natural numbers when N is extremely large
- Large Fibonacci Numbers in Java
- Sort an array of large numbers
- Find Last Digit of a^b for Large Numbers
- Subtraction of two large numbers using 9's compliment
- Subtraction of two large numbers using 10's compliment
- Sum of two large Floating-point numbers
- Sum of all natural numbers from L to R ( for large values of L and R )
- Last digit of Product of two Large or Small numbers (a * b)
- Fermat's Factorization method for large numbers
- Factorial of Large numbers using Logarithmic identity
- How to generate Large Prime numbers for RSA Algorithm
- Multiply Large Numbers represented as Strings
- Multiply Large Numbers using Grid Method
- Writing power function for large numbers