public class GCD { public static void main(String[] args) { //Example how to use this method System.out.println(GCD(15,50)); } //find the greatest common divisor of two numbers public static int GCD(int a, int b){ if (b == 0) return a; return GCD(b, a % b); } } Hope this help to solve you problem.
There are many ways of solving this problem using primitive types. However, if you are willing to sacrifice some memory, you can use BigInteger's gcd() method to make things easy. Example: public static int gcd (int[] numbers) { BigInteger k = BigInteger.ZERO; for (int n : numbers) k = k.gcd(new BigInteger(""+n)); return k.intValue(); }
To find the GCD of three numbers, a, b and c, you need to find the GCD of a and b first, such that d = GCD(a, b). Then call GCD(d, c). Although you could simply call GCD(GCD(a, b), c), a more useful method is to use an array and iteratively call the GCD(a, b) function, such that a and b are the first two numbers in the first iteration, which becomes a in the next iteration, while b is the next number. The following program demonstarates this method. Note that the GCD of two numbers can either be calculated recursively or iteratively. This program includes both options, depending on whether RECURSIVE is defined or not. In a working program you'd use one or the other, but the iterative approach is usually faster because it requires just one function call and no additional stack space. The program will create 10 random arrays of integers of length 3 to 5 and process each in turn. Note that the more numbers in the array, the more likely the GCD will be 1. #include<iostream> #include<time.h> #define RECURSIVE // comment out to use iterative method #define ARRAY // comment out to use non-arrays #ifdef RECURSIVE // Returns the GCD of the two given integers (recursive method) unsigned int gcd(unsigned int a, unsigned int b) { if(!a) return(b); if(!b) return(a); if(a==b) return(a); if(~a&1) { if(b&1) return(gcd(a>>1,b)); else return(gcd(a>>1,b>>1)<<1); } if(~b&1) return(gcd(a,b>>1)); if(a>b) return(gcd((a-b)>>1,b)); return(gcd((b-a)>>1,a)); } #else // Returns the GCD of the two given integers (iterative method) unsigned int gcd(unsigned int a, unsigned int b) { if(!a) return(b); if(!b) return(a); int c; for(c=0; ((a|b)&1)==0; ++c) { a>>=1; b>>=1; } while((a&1)==0) a>>=1; do{ while((b&1)==0) b>>=1; if(a>b) { unsigned int t=a; a=b; b=t; } b-=a; }while(b); return(a<<c); } #endif RECURSIVE // Returns the greatest common divisor in the given array unsigned int gcd(const unsigned int n[], const unsigned int size) { if( size==0 ) return( 0 ); if( size==1 ) return( n[0] ); unsigned int hcf=gcd(n[0],n[1]); for( unsigned int index=2; index<size; ++index ) hcf=gcd(hcf,n[index]); return(hcf); } int main() { using std::cout; using std::endl; srand((unsigned) time(NULL)); for(unsigned int attempt=0; attempt<10; ++attempt) { unsigned int size=rand()%3+3; unsigned int* num = new unsigned int[size]; unsigned int index=0; while(index<size) num[index++]=rand()%100; unsigned int hcf=gcd(num,size); cout<<"GCD("; index=0; cout<<num[index]; while(++index<size) cout<<','<<num[index]; cout<<") = "<<hcf<<endl; delete[]num; } cout<<endl; }
algorithm GCD (a, b) is:while (a b) doif a > b then a := a - b else b := b - aend whilereturn a
The following function will return the GCD or LCM of two arguments (x and y) depending on the value of the fct argument (GCD or LCM). enum FUNC {GCD, LCM}; int gcd_or_lcm(FUNC fct, int x, int y) { int result = 0; switch (fct) { case (GCD): result = gcd (x, y); break; case (LCM): result = lcm (x, y); break; } return result; }
public class GCD { public static void main(String[] args) { //Example how to use this method System.out.println(GCD(15,50)); } //find the greatest common divisor of two numbers public static int GCD(int a, int b){ if (b == 0) return a; return GCD(b, a % b); } } Hope this help to solve you problem.
if the gcd and lcm are given and one of the numbers are also given,multiply the gcd and lcm and divide them by the given number
The GCD is 2.
GCD is simply a mathematical operator. You can define any operator on one or more inputs. That is their definition - they do not require justification.
write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? write a shell program for finding out gcd of three given numbers? check bellow link http://bashscript.blogspot.com/2009/08/gcd-of-more-than-two-numbers.html
Greatest Common Divisor (GCD) for 18 45 72 is 9.
use slide
The GCF is 1.
Euclid's algorithm is a popular algorithm to compute the GCD of two numbers. Algorithm: Gcd(a,b) = Gcd(b, a mod b), where a>=b and Gcd(a,0) = a Say we want to find the GCD of 72 and 105. 105 mod 72 = 33, so GCD(72,105) = GCD(33,72) 72 mod 33 = 6, so GCD(33,72) = GCD(6,33) 33 mod 6 = 3 so GCD(6,33) = GCD(3,6) 6 mod 3 = 0 so GCD(3,6) = GCD(0,3) = 3. So the GCD of 72 and 105 is 3.
Many numbers could have 18 as their GCD (greatest common demoninator/divisor). for instance, 18 and 36 have 18 as their GCD. In general, if you have 2 numbers who's GCD is 18, then those numbers could be expressed as 18x and 18y such that x and y are relatively prime (share no factors, other than 1).
Any two numbers who are relatively prime will workSo look at 9 and 4. Neither is prime and their GCD is 1.You must need two numbers with NO other factors in common.
You need at least two numbers to find a GCF.