Greatest common divisor of multiple numbers

Source: Internet
Author: User
Tags greatest common divisor

---restore content starts---

A book that is looking at an algorithm recently. It's all about basic questions that don't involve very complex algorithms, or that the book is more about skill.

One of the opening of the greatest common divisor of the algorithm, feel that there is merit, and share with you.

When it comes to greatest common divisor, the first thing we think of is the method of dividing.

Yes, never despise our ancestors, their wisdom is endless. (Pull away, hey)

We are all using the method of greatest common divisor, but seldom to think about why the division of the law can be asked to greatest common divisor it? Or how to prove the correctness of the algorithm (at least I haven't thought about it before).

Here we have a perceptual understanding of the Euclidean method (not very strictly to prove it).

Assume two numbers, a, B, and A>b. Set a divided by B quotient K, the remainder of R, then there will be a=k*b+r, then B and r greatest common divisor, is a and B greatest common divisor. So the problem is to convert the precipitant divisor and the remainder of the greatest common divisor, in turn recursive, recursive exit is a known condition: If a can be divisible by B, then B is a and B greatest common divisor, so the division of the recursive code is as follows:

int GCD1 (int num1,int num2)
{
if (num1%num2==0)
{
return num2;
}
Else
{
int next1=num2;
int next2=num1%num2;
Return GCD1 (NEXT1,NEXT2);
}
}

There is also a familiar greatest common divisor algorithm is more subtractive damage, his basic idea is: two number, a, and a>b, then make C=a-b, and then the B and C as a new A and B, recursion, recursive exit is a known condition: if a=b, So the greatest common divisor of a and B are either A or B. in fact, the more subtractive loss and the division of the method is an east, more subtraction is to let the division of the Law of the quotient (k) constant of 1, so in most cases, the efficiency of the Division method is more efficient than the more subtractive loss. give the code for a more subtractive technique:

int GCD2 (int num1,int num2)
{
if (num1==num2)
{
return num2;
}
Else
{
int next1= (NUM1>NUM2)? (num1-num2):(num2-num1);
int next2= (num1>num2)? Num2:num1;
if (NEXT1>NEXT2)
{
Return GCD2 (NEXT1,NEXT2);
}
Else
{
Return GCD2 (NEXT2,NEXT1);
}
}
}

OK, step into this topic: the number of greatest common divisor (in fact, is the expansion of the Division method) to give the algorithm:

Set a number of A1,A2,A3,A4,A5.

(1) Sort this group of numbers (from big to small)

(2) The following operations are performed on each of the two adjacent two numbers:

Set the adjacent two numbers to A and B (a in front, because it is already sorted, so a>b), if A=n*b (n is an integer), that is, a can be divisible by B, then a=b; If a cannot be divisible by B then a=a%b.

(3) Repeat (1), (2) know that each number in the array is equal, then the greatest common divisor is the number.

Give the complete program:

#include <iostream>
using namespace Std;
void Sort (int* num,int N);
int GCD3 (const int* num,int n);
int main ()
{

  int num[4]={756,504,630,2226};
  int result=gcd3 (num,4);
  cout<< "array:";
  for (int i=0;i<4;i++)
  {
   cout<<num[i]<< " ";
 }
  cout<< "greatest common divisor:" <<RESULT<<ENDL;
  return 0;
}

Int GCD3 (const int* num,int N)
{
  int *temp=new int[n];
  for (int i=0;i<n;i++)
  {
   temp[i]=num[i];
 
  do
  { 
   if (temp[0]==temp[n-1])
   {
  &      nbsp Break
  }
   Else
   {
    sort (temp,n);//Sort
    for ( int i=0;i<n-1;i++)
    {
     if (temp[i]%temp[i+1]==0)
&NBSP;&N bsp;   {
      temp[i]=temp[i+1];
    
     Else
     {
&NBSP;&N bsp;    temp[i]=temp[i]%temp[i+1];
    }
   }
  }
 }while (temp[0]!=temp[n-1]);
  return temp[0];
}

Sort
void Sort (int* num,int N)
{

Bubble Sort Method
for (int i=0;i<n-1;i++)
{
for (int j=i;j<n-1;j++)
{
if (num[i]<num[j+1])
{
int temp=num[i];
NUM[I]=NUM[J+1];
Num[j+1]=temp;
}
}
}
}

---restore content ends---

Greatest common divisor of multiple numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.