sfafa
I guess you mean the highest common factor. Use a loop. Start assuming that the hcf is the first number in the array. Call this "result". Find the hcf of "result" and the second element, assign this to "result". Find the hcf of "result" and the third element, and copy this back to result, etc.
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; }
#define SIZE 100 #include <stdio.h> int hcf_function(int,int); int lcm_function(int,int); int main() { int array[SIZE],n,i,choice,LCM,hcf; printf("Enter No of Elements\n"); scanf("%d",&n); printf("Enter Elements\n"); for(i=0;i<n;i++) scanf("%d",&array[i]); do { printf("\n\nEnter Choice\n\n1.HCF\n2.LCM\n3.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: hcf=array[0]; for(i=1;i<n;i++) hcf=hcf_function(hcf,array[i]); printf("\nHCF = %d",hcf); break; case 2: LCM=array[0]; for(i=1;i<n;i++) LCM=lcm_function(LCM,array[i]); printf("\nLCM = %d",LCM); break; case 3: break; default:printf("Wrong Choice"); break; } }while(choice!=3); } /*************************************************************** Function Name : hcf_function Purpose : to find hcf Input : two numbers Return Value : hcf Return Type : int ****************************************************************/ int hcf_function(int m,int n) { int temp,reminder; if(m<n) { temp=m; m=n; n=temp; } while(1) { reminder=m%n; if(reminder==0) return n; else m=n; n=reminder; } } /*************************************************************** Function Name : lcm_function Purpose : to find LCM Input : two numbers Return Value : LCM Return Type : int ****************************************************************/ int lcm_function(int m,int n) { int LCM; LCM=m*n/hcf_function(m,n); return LCM; }
Dunno about Linux, but I've written mine in C.It prime factorises the numbers, making a note of the highest power of each prime factor as it goes (in a linked list of malloc()ed structures). Once all the numbers have been factorised, it has a list of all the primes used along with their highest power. The lcm is then the product of the primes raised to their highest power.You are also not limited to the lcm of 2 numbers - you can keep factorising numbers until you run out of them and find the lcm of them all!Whilst you're at it you can add finding their hcf very easily: this time it's the product of the common primes to their lowest power.All that is then needed is the prime factorisation of the numbers.The normal method is:try the first prime (2)If it does not divide the number: set the prime to the next primeTry again from step 2Add one to the power count of this primereplace the number by the number divided by the primeif the number is not 1 go back to step 2Found all primes, stop!Finding the primes by which to divide is not easy on the fly, so you could check 2 specifically and then all odd numbers 3, 5, 7,..., but an improvement is to specifically check 2 and 3 and then check the numbers 6n ± 1 (which may be prime and why are 6n, 6n ± 2 and 6n ± 3 definitely not prime?) which skips every third odd number - this sequence of potential primes (5, 7, 11, 13, 17, 19, ...) can be easily generated.And while you're at it, you could display the prime factorisation you've done.And using that prime factorisation you can list the factors (and factor pairs) for the numbers.Obviously you'll need to sort out how the numbers are input to the program - I decode argv[], but you could use reading from stdin if you prefer.
First you will need to pick out the two numbers. Then you can use your textbook and the instructions in order to draw out the flow chart.
sfafa
The HCF of the given two numbers is 40
The HCF of the given numbers is 1
The HCF of the given numbers is 14
The HCF of the given two numbers is 14
The HCF of the given numbers is 237
The HCF of the given three numbers is 9 and because the digital sum of the given numbers equals 9 then the numbers are all divisible by 9
The HCF of the given numbers is 25
The HCF of the given two numbers is 7
The HCF of the given numbers is 13
The HCF of the given two numbers is 10