How To Find Gcd Of A Number
GCD (Greatest Mutual Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them.
For example GCD of 20 and 28 is 4 and GCD of 98 and 56 is 14. For solution suppose a=98 & b=56
a>b so put a= a-b and b is remain same and so a=98-56=42 & b= 56 . Now b>a so b=b-a and a is same
b= 56-42 = xiv & a= 42 . 42 is three times of 14 so HCF is 14 . besides a=36 & b=60 ,here b>a so b = 24 & a= 36 at present a>b so a= 12 & b= 24 . 12 is HCF of 36 and threescore . This concept is always satisfying.
A simple solution is to observe all prime factors of both numbers, so observe intersection of all factors present in both numbers. Finally return product of elements in the intersection.
An efficient solution is to use Euclidean algorithm which is the main algorithm used for this purpose. The thought is, GCD of two numbers doesn't change if smaller number is subtracted from a bigger number.
C++
#include <iostream>
using
namespace
std;
int
gcd(
int
a,
int
b)
{
if
(a == 0)
return
b;
if
(b == 0)
render
a;
if
(a == b)
return
a;
if
(a > b)
return
gcd(a-b, b);
return
gcd(a, b-a);
}
int
main()
{
int
a = 98, b = 56;
cout<<
"GCD of "
<<a<<
" and "
<<b<<
" is "
<<gcd(a, b);
return
0;
}
C
#include <stdio.h>
int
gcd(
int
a,
int
b)
{
if
(a == 0)
return
b;
if
(b == 0)
render
a;
if
(a == b)
return
a;
if
(a > b)
return
gcd(a-b, b);
render
gcd(a, b-a);
}
int
main()
{
int
a = 98, b = 56;
printf
(
"GCD of %d and %d is %d "
, a, b, gcd(a, b));
return
0;
}
Java
class
Test
{
static
int
gcd(
int
a,
int
b)
{
if
(a ==
0
)
return
b;
if
(b ==
0
)
return
a;
if
(a == b)
return
a;
if
(a > b)
return
gcd(a-b, b);
return
gcd(a, b-a);
}
public
static
void
principal(String[] args)
{
int
a =
98
, b =
56
;
System.out.println(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
}
}
Python3
def
gcd(a,b):
if
(a
=
=
0
):
return
b
if
(b
=
=
0
):
return
a
if
(a
=
=
b):
return
a
if
(a > b):
return
gcd(a
-
b, b)
render
gcd(a, b
-
a)
a
=
98
b
=
56
if
(gcd(a, b)):
print
(
'GCD of'
, a,
'and'
, b,
'is'
, gcd(a, b))
else
:
print
(
'not institute'
)
C#
using
System;
class
GFG {
static
int
gcd(
int
a,
int
b)
{
if
(a == 0)
return
b;
if
(b == 0)
render
a;
if
(a == b)
return
a;
if
(a > b)
return
gcd(a - b, b);
return
gcd(a, b - a);
}
public
static
void
Main()
{
int
a = 98, b = 56;
Console.WriteLine(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
}
}
PHP
<?php
function
gcd(
$a
,
$b
)
{
if
(
$a
== 0)
return
$b
;
if
(
$b
== 0)
return
$a
;
if
(
$a
==
$b
)
return
$a
;
if
(
$a
>
$b
)
return
gcd(
$a
-
$b
,
$b
) ;
return
gcd(
$a
,
$b
-
$a
) ;
}
$a
= 98 ;
$b
= 56 ;
repeat
"GCD of $a and $b is "
, gcd(
$a
,
$b
) ;
?>
Javascript
<script>
function
gcd(a, b)
{
if
(a == 0)
render
b;
if
(b == 0)
render
a;
if
(a == b)
return
a;
if
(a > b)
render
gcd(a-b, b);
render
gcd(a, b-a);
}
permit a = 98, b = 56;
document.write(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
</script>
Output:
GCD of 98 and 56 is fourteen
Time Complication: O(max(a,b))
Auxiliary Space: O(max(a,b))
Dynamic Programming Approach (Meridian Downwardly Using Memoization) :
C++
#include <bits/stdc++.h>
using
namespace
std;
int
static
dp[1001][1001];
int
gcd(
int
a,
int
b)
{
if
(a == 0)
return
b;
if
(b == 0)
return
a;
if
(a == b)
return
a;
if
(dp[a][b] != -1)
return
dp[a][b];
if
(a > b)
dp[a][b] = gcd(a-b, b);
else
dp[a][b] = gcd(a, b-a);
render
dp[a][b];
}
int
main()
{
int
a = 98, b = 56;
memset
(dp, -1,
sizeof
(dp));
cout<<
"GCD of "
<<a<<
" and "
<<b<<
" is "
<<gcd(a, b);
render
0;
}
Java
import
coffee.util.*;
public
class
GFG
{
static
int
[][]dp =
new
int
[
1001
][
1001
];
static
int
gcd(
int
a,
int
b)
{
if
(a ==
0
)
return
b;
if
(b ==
0
)
return
a;
if
(a == b)
return
a;
if
(dp[a][b] != -
one
)
return
dp[a][b];
if
(a > b)
dp[a][b] = gcd(a-b, b);
else
dp[a][b] = gcd(a, b-a);
return
dp[a][b];
}
public
static
void
master(Cord[] args)
{
for
(
int
i =
0
; i <
1001
; i++) {
for
(
int
j =
0
; j <
1001
; j++) {
dp[i][j] = -
1
;
}
}
int
a =
98
, b =
56
;
System.out.println(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
}
}
Python3
dp
=
[[
-
1
for
i
in
range
(
1001
)]
for
j
in
range
(
1001
)]
def
gcd(a,b):
if
(a
=
=
0
):
return
b
if
(b
=
=
0
):
render
a
if
(a
=
=
b):
return
a
if
(dp[a][b] !
=
-
1
):
return
dp[a][b]
if
(a > b):
dp[a][b]
=
gcd(a
-
b, b)
else
:
dp[a][b]
=
gcd(a, b
-
a)
return
dp[a][b]
a
=
98
b
=
56
if
(gcd(a, b)):
print
(
'GCD of'
, a,
'and'
, b,
'is'
, gcd(a, b))
else
:
print
(
'not found'
)
C#
using
System;
grade
GFG
{
static
int
[,]dp =
new
int
[1001, 1001];
static
int
gcd(
int
a,
int
b)
{
if
(a == 0)
return
b;
if
(b == 0)
return
a;
if
(a == b)
return
a;
if
(dp[a, b] != -1)
return
dp[a, b];
if
(a > b)
dp[a, b] = gcd(a-b, b);
else
dp[a, b] = gcd(a, b-a);
return
dp[a, b];
}
public
static
void
Chief()
{
for
(
int
i = 0; i < 1001; i++) {
for
(
int
j = 0; j < 1001; j++) {
dp[i, j] = -1;
}
}
int
a = 98, b = 56;
Panel.Write(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
}
}
Javascript
var
dp =
new
Array(1001);
for
(
var
i = 0; i < dp.length; i++) {
dp[i] =
new
Array(1001);
}
office
gcd(a, b)
{
if
(a == 0)
return
b;
if
(b == 0)
return
a;
if
(a == b)
return
a;
if
(dp[a][b] != -i)
return
dp[a][b];
if
(a > b)
dp[a][b] = gcd(a-b, b);
else
dp[a][b] = gcd(a, b-a);
return
dp[a][b];
}
permit a = 98, b = 56;
for
(allow i = 0; i < 1001; i++) {
for
(let j = 0; j < 1001; j++) {
dp[i][j] = -one;
}
}
certificate.write(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
</script>
Output
GCD of 98 and 56 is 14
Fourth dimension Complexity: O(max(a,b))
Auxiliary Space: O(i)
A more than efficient solution is to utilize modulo operator in Euclidean algorithm.
C++
#include <iostream>
using
namespace
std;
int
gcd(
int
a,
int
b)
{
return
b == 0 ? a : gcd(b, a % b);
}
int
master()
{
int
a = 98, b = 56;
cout<<
"GCD of "
<<a<<
" and "
<<b<<
" is "
<<gcd(a, b);
return
0;
}
C
#include <stdio.h>
int
gcd(
int
a,
int
b)
{
if
(b == 0)
return
a;
return
gcd(b, a % b);
}
int
chief()
{
int
a = 98, b = 56;
printf
(
"GCD of %d and %d is %d "
, a, b, gcd(a, b));
return
0;
}
Java
class
Test
{
static
int
gcd(
int
a,
int
b)
{
if
(b ==
0
)
return
a;
return
gcd(b, a % b);
}
public
static
void
main(String[] args)
{
int
a =
98
, b =
56
;
Organization.out.println(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
}
}
Python3
def
gcd(a,b):
if
(b
=
=
0
):
return
a
return
gcd(b, a
%
b)
a
=
98
b
=
56
if
(gcd(a, b)):
print
(
'GCD of'
, a,
'and'
, b,
'is'
, gcd(a, b))
else
:
impress
(
'not establish'
)
C#
using
System;
class
GFG {
static
int
gcd(
int
a,
int
b)
{
if
(b == 0)
return
a;
return
gcd(b, a % b);
}
public
static
void
Master()
{
int
a = 98, b = 56;
Console.WriteLine(
"GCD of "
+ a +
" and "
+ b +
" is "
+ gcd(a, b));
}
}
PHP
<?php
function
gcd(
$a
,
$b
)
{
if
(
$b
==0)
return
$a
;
return
gcd(
$b
,
$a
%
$b
) ;
}
$a
= 98 ;
$b
= 56 ;
echo
"GCD of $a and $b is "
, gcd(
$a
,
$b
) ;
?>
Javascript
<script>
office
gcd(a, b){
if
(b == 0){
return
a;
}
return
gcd(b, a % b);
}
let a = 98;
let b = 56;
document.write(`GCD of ${a} and ${b} is ${gcd(a, b)}`);
</script>
Output:
GCD of 98 and 56 is 14
Time Complication: O(log(max(a,b))
Auxiliary Space: O(log(max(a,b))
The time complexity for the to a higher place algorithm is O(log(max(a,b))) the derivation for this is obtained from the analysis of the worst-case scenario. What we practise is we inquire what are the 2 to the lowest degree numbers that accept 1 footstep, those would be (i,1). If we want to increase the number of steps to 2 while keeping the numbers as low as possible as nosotros tin take the numbers to be (i,2). Similarly, for 3 steps, the numbers would exist (2,three), four would be (3,5), 5 would exist (5,8). So we can notice a pattern here, for the nth step the numbers would exist (fib(n),fib(n+ane)). So the worst-case fourth dimension complexity would exist O(n) where a>= fib(n) and b>= fib(n+1).
Now Fibonacci series is an exponentially growing series where the ratio of nth/(n-1)th term approaches (sqrt(v)-1)/2 which is also chosen the golden ratio. So we can see that the time complexity of the algorithm increases linearly as the terms grow exponentially hence the time complexity would be log(max(a,b)).
Please refer GCD of more than than two (or assortment) numbers to find HCF of more than than two numbers.
Please write comments if you discover anything incorrect, or you want to share more information about the topic discussed above
Source: https://www.geeksforgeeks.org/c-program-find-gcd-hcf-two-numbers/
Posted by: sowellholed1992.blogspot.com
0 Response to "How To Find Gcd Of A Number"
Post a Comment