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;
}
draw a flowchart to find the biggest number among the 3 numbers
Cls input "enter two no.s ",a,b sum=a+b print "sum = ";sum end
create a program that iterates until it finds a perfect number, then store that perfect number into an array. Continue iterating until you find three more. Then, you have an array of four perfect numbers.
(defun max3 (a b c) (cond ((> a b) (cond ((> a c) a) (t c))) ((> b c) b) (t c) ) )
#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("enter the three numbers: "); scanf(%d%d%d:,&a&b&c); if(a>b | a>c) { printf("the greatest no: is %d",a); } if(b>c) { printf("the greatest no: is %d",b); } else { printf("the greatest no: is %d",c); } getch(); }
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
Given only one number it is impossible to identify three missing numbers.
The given three numbers are all prime numbers
The LCM of the given three numbers is 48
Their given! You found it! Boom
The LCM of the given three numbers is 168
You add the three numbers, then divide the result by 3.
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 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.
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
Adding 0.6 each time gives 5.2, 5.8 and 6.4 as the next three numbers.
Pythagorean triple