GCD of two numbers when one of them can be very large
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++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll gcd(ll a, ll b)
{
if (!a)
return b;
return gcd(b % a, a);
}
ll reduceB(ll a, char b[])
{
ll mod = 0;
for ( int i = 0; i < strlen (b); i++)
mod = (mod * 10 + b[i] - '0' ) % a;
return mod;
}
ll gcdLarge(ll a, char b[])
{
ll num = reduceB(a, b);
return gcd(a, num);
}
int main()
{
ll a = 1221;
char b[] = "1234567891011121314151617181920212223242526272829" ;
if (a == 0)
cout << b << endl;
else
cout << gcdLarge(a, b) << endl;
return 0;
}
|
Java
class GFG
{
private static int gcd( int reduceNum, int b)
{
return b == 0 ?
reduceNum : gcd(b, reduceNum % b);
}
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)
{
int num = reduceB(a, b);
return gcd(num, a);
}
public static void main(String[] args)
{
int a = 1221 ;
String b = "19837658191095787329" ;
if (a == 0 )
System.out.println(b);
else
System.out.println(gcdLarge(a, b));
}
}
|
Python3
def gcd(a, b) :
if (a = = 0 ) :
return b
return gcd(b % a, a)
def reduceB(a, b) :
mod = 0
for i in range ( 0 , len (b)) :
mod = (mod * 10 + ord (b[i])) % a
return mod
def gcdLarge(a, b) :
num = reduceB(a, b)
return gcd(a, num)
a = 1221
b = "1234567891011121314151617181920212223242526272829"
if a = = 0 :
print (b)
else :
print (gcdLarge(a, b))
|
C#
using System;
class GFG
{
public long gcd( long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
public long reduceB( long a, string b)
{
long mod = 0;
for ( int i = 0; i < b.Length; i++)
mod = (mod * 10 +
(b[i] - '0' )) % a;
return mod;
}
public long gcdLarge( long a, string b)
{
long num = reduceB(a, b);
return gcd(a, num);
}
static void Main()
{
long a = 1221;
string b = "1234567891011121314151617181920212223242526272829" ;
GFG p = new GFG();
if (a == 0)
Console.WriteLine(b);
else
Console.WriteLine(p.gcdLarge(a, b));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if (! $a )
return $b ;
return gcd( $b % $a , $a );
}
function reduceB( $a , $b )
{
$mod = 0;
for ( $i = 0; $i < strlen ( $b ); $i ++)
$mod = ( $mod * 10 +
$b [ $i ] - '0' ) % $a ;
return $mod ;
}
function gcdLarge( $a , $b )
{
$num = reduceB( $a , $b );
return gcd( $a , $num );
}
$a = 1221;
$b = "1234567891011121314151617181920212223242526272829" ;
if ( $a == 0) {
echo ( $b );
}
else {
echo gcdLarge( $a , $b );
}
?>
|
Javascript
<script>
function gcd( a, b)
{
if (!a)
return b;
return gcd(b%a,a);
}
function reduceB( a, b)
{
let mod = 0;
for (let i=0; i<b.length-1; i++)
mod = (mod*10 + b[i] - '0' )%a;
return mod;
}
function gcdLarge( a, b)
{
let num = reduceB(a, b);
return gcd(a, num);
}
let a = 1221;
let b = "1234567891011121314151617181920212223242526272829" ;
if (a == 0)
document.write( b);
else
document.write(gcdLarge(a, b));
</script>
|
Time Complexity: O(log (min(a,b)))
Auxiliary Space: O(log (min(a,b)))
This article is reviewed by team GeeksforGeeks .
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...