Gcd Improvized Method(euclid Division Method/long Division Method)
note:coded using while and for loops check both programs.
[Success] Your code was executed successfully
Actually, in the general method it takes too much time since we check until the minimum of the both numbers and find the maximum/greatest common divisor. but in this EUCLID DIVISION OR LONG DIVISION METHOD the number of steps is minimized. so,this is less complex and faster than that algorithm.
using while loop
class Main
{
public static int gcd(int A,int B)
{
int a=A;
int b=B;
int a_,b_;
while(a!=0)
{
a_= b%a;
b_=a;
a = a_;
b = b_;
}
return b;
}
public static void main(String args[])
{
int res= gcd(6,15);
System.out.println("gcd is "+ res);
}
}
[Success] Your code was executed successfully
gcd is 3
using for loop
class Main
{
public static int gcd(int A,int B)
{
int a=A;
int b=B;
int a_,b_;
for( ;a!=0; )
{
a_=b%a;
b_=a;
a=a_;
b=b_;
}
return b;
}
public static void main(String args[])
{
int res= gcd(6,15);
System.out.println("gcd is "+ res);
}
}
[Success] Your code was executed successfully
gcd is 3