Two methods for finding greatest common divisor (Euclidean algorithm and prime factorization)

Source: Internet
Author: User
Tags gcd greatest common divisor pow

Two methods of greatest common divisor (Euclidean algorithm and prime factorization) method one: Euclidean algorithm, also known as the Division method

Theorem (Euclidean algorithm): Set A and B are positive integers, there is a maximum of the maximum common factor d= (A, b) an algorithm, and exist to find a set of integers s,t make d = SA+TB

For example: Ask for the greatest common divisor of 168 and 60?

168 = 2 * 60 + 48

60 = 1 * 48 +12

48 = 4 * 12

The result is a greatest common divisor of 12.

About Max Common multiple

C Language Program code: Very simple, no comment.

#include <stdio.h> #define SWAP (A, b) {a=a+b; b=a-b; a=a-b;} int gcd (int a,int b) {if (a==b) return a;if (a<b) SWAP (A, b), int temp = 0;while (a%b) {temp = B;b = a% B;a = temp;} return b;} int main () {int a = 168;int B = 60;printf ("Greatest common divisor is:%d\n", gcd (A, b)); return 0;}
Method Two: Prime factorization

We know that every integer greater than or equal to 2 can be expressed as a form of multiplication of several primes (arithmetic basic theorem) and has its corresponding unique decomposition, which we can also use to solve the maximum common factor (greatest common divisor)

For example: In the above title 168 can be represented as POW (2,3) *pow (3,1) *pow (5,0) *pow (7,1)

60 can be represented as POW (2,2) *pow (3,1) *pow (5,1) *pow (7,0)

So (168,60) = pow* (2,2) *pow (3,1) *pow (5,0) *pow (7,0) = 12

 #include < stdio.h> #include <math.h> #define COUNT_PRIME 10void Divisor (int num,int prime[],int index,int& loop)// The number of factors to be decomposed {if (num==1) return;int count = 0;while (num%index==0) {num/= index;count++;} if (index==2) {Prime[index-2] = Count;index + = 1;} ELSE{PRIME[INDEX/2] = Count;index + = 2;} Divisor (Num,prime,index,loop); loop + = 1;} int Gcd (int a[],int b[],int loop_a,int loop_b)//solve maximum common factor {int Gcd = A[0]<B[0]?POW (2,a[0]):p ow (2,b[0]); for (int i=1,j=3; i<loop_a| | i<loop_b;i++,j+=2) {int num = A[i]<b[i]?pow (j,a[i]):p ow (J,b[i]); GCD *= num;} return GCD;} int main () {int a = 60,b = 168;int Prime_a[count_prime] = {0};int Prime_b[count_prime] = {0};int loop_a = 0,loop_b = 0;Divi Sor (a,prime_a,2,loop_a);D ivisor (b,prime_b,2,loop_b), int gcd = GCD (prime_a,prime_b,loop_a,loop_b);p rintf ("Greatest common divisor is:% D\n ", GCD);p rintf (" Maximum common multiple is:%d\n ", A*B/GCD); return 0;} 

The main function here is to find the number of factors, I use the recursive return to solve the problem

Two methods for finding greatest common divisor (Euclidean algorithm and prime factorization)

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.