answersLogoWhite

0

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;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Shell program for 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? 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


What numbers can 3 8 and 16 go into?

The LCM of the given three numbers is 48


Are 83 43 and 61 prime?

The given three numbers are all prime numbers


How to find ratio between three numbers when their ratios are given?

Their given! You found it! Boom


What numbers go 6 7 and 8 go into?

The LCM of the given three numbers is 168


How do you find missing number if the mean is given with two numbers?

The mean times three will be the total of all three numbers. Multiply the mean times three and subtract the sum of the two numbers from that total.


What are the next three numbers in the pattern of 2071 2141 2211?

What numbers would you like to be the next three? It is possible to find a polynomial of order 5 that will fit these three numbers and ANY other three numbers. For example, using the positin to value rule given by Un= -112.275n5 + 1,966.25n4 - 12,744.625n3 + 37,416.25n2 - 48,979.6 + 36,525 I can fit the numbers 1, 2 and 3 as the next three. So there are infinitely many possible answers to the question. Using the simplest rule, although the question does not require that, gives Un = 70n + 2001 and that requires the next three numbers to be 2281, 2351, 2421.


How do you find average of three numbers in 8085 microprocessor code in a given address and data?

You add the three numbers, then divide the result by 3.


What is the name given to three whole numbers that can be the three sides of a right angled triangle?

Pythagorean triple


What are the next three number in 63605651?

Answer: 453 The number given can be seen as a sequence of 63 60 56 51. Taking these as separate numbers: 60 is 63 minus 3 56 is 60 minus 4 51 is 56 minus 5 the numbers decrease by the (difference between the last two numbers plus 1) So, 51 minus 6 gives 45 and 45 minus 7 gives 38 The next three numbers are, therefore, 453


What are the next three numbers 2.8 3.4 4 4.6?

Adding 0.6 each time gives 5.2, 5.8 and 6.4 as the next three numbers.


If there is only three numbers what is the median?

When you put the numbers in numerical order, it is the middle number. For example: you are given these three numbers- 9,3,5 put in numerical order: 3,5,9 median is 5.