Three algorithms for finding the maximum Common Divisor

Source: Internet
Author: User

/*************************************** **************/
/**/
/* Three algorithms for finding the maximum common divisor great common divisor */
/* 1. Use Euclidean algorithms */
/* 2. Use the continuous integer detection algorithm */
/* 3. Use the algorithm of the middle school age (using the heratosini screen )*/
/* Author: lixiongwei */
/* Time: 06/11/11 sun .*/
/* Win XP + (TC/win_tc/VC + + 6.0 )*/
/**/
/*************************************** **************/
# Include <stdio. h>
# Include <conio. h>
# Include <stdlib. h>
# Deprecision Max 5000
/****************** Function prototype declaration ****************** *****/
Unsigned int my_gcd1 (unsigned int M, unsigned int N );
Unsigned int my_gcd2 (unsigned int M, unsigned int N );
Unsigned int my_gcd3 (unsigned int M, unsigned int N );
Int my_sieve (unsigned int N, unsigned int * l );

Int main ()
{
Unsigned int M, N;

Printf ("Please enter two number :");
Scanf ("% u", & M, & N );
Printf ("/ngreat common divisor: my_gcd1 (% u, % u) = % u/n", M, N, my_gcd1 (m, n ));
Printf ("great common divisor: my_gcd2 (% u, % u) = % u/n", M, N, my_gcd2 (m, n ));
Printf ("great common divisor: my_gcd3 (% u, % u) = % u/n", M, N, my_gcd3 (m, n ));

Getch ();
Return 0;
}

/****** Use the Euclidean Algorithm function: my_gcd1 () definition section *******/
Unsigned int my_gcd1 (unsigned int M, unsigned int N)
{
Unsigned int temp = m;
Unsigned int R;

If (M <N)/* swap M, N */
{
M = N;
N = temp;
}

If (0 = m)
{
Printf ("You must enter one number much than zero! ");
Getch ();
Exit (1);/* abnormity */
}

While (n! = 0)
{
R = m % N;
M = N;
N = R;
}

Return m;
}

/***** Use the continuous integer detection algorithm function: my_gcd2 () definition section *****/
Unsigned int my_gcd2 (unsigned int M, unsigned int N)
{
Unsigned int T;

If (0 = m) & (0 = n ))
{
Printf ("You must enter one number much than zero! ");
Getch ();
Exit (1);/* abnormity */
}
If (0 = m)
Return N;
If (0 = N)
Return m;

T = (M <n )? M: N;
While (1)
{
If (M % T) = 0) & (n % T) = 0 ))
Break;
Else
-- T;
}

Return T;
}

/***** Use the algorithm of the middle school age (using the heratosini screen) function: my_gcd3 () definition section ****/
Unsigned int my_gcd3 (unsigned int M, unsigned int N)
{
Unsigned int ml [Max];
Unsigned int nl [Max];
Unsigned int Mr [Max];
Unsigned int Nr [Max];
Unsigned int T = 1;
Int I, JM, JN, mi, ni;
Int ZM = 0, Zn = 0;

If (0 = m) & (0 = n ))
{
Printf ("You must enter one number much than zero! ");
Getch ();
Exit (1);/* abnormity */
}
If (0 = m)
Return N;
If (0 = N)
Return m;

For (I = 0; I <Max; ++ I)
{
ML [I] = 0;
NL [I] = 0;
Mr [I] = 0;
NR [I] = 0;
}

Mi = my_sieve (M, ML );
Ni = my_sieve (n, NL );

I = 0; JM = 0;
While (I <mi)
{
While (1)
{
If (M % ML [I]) = 0)
{
Mr [JM] = ML [I];
M = m/ml [I];
++ JM;
}
Else
Break;
}/* Inside while end */
++ I;
}/* Out while end */

I = 0; JN = 0;
While (I <Ni)
{
While (1)
{
If (n % nl [I]) = 0)
{
NR [JN] = nl [I];
N = N/nl [I];
++ JN;
}
Else
Break;
}/* Inside while end */
++ I;
}/* Out while end */

For (ZM = 0, Zn = 0; (ZM <JM) & (Zn <JN );)
{
If (Mr [ZM] <Nr [Zn])
++ ZM;
If (Mr [ZM]> Nr [Zn])
+ + Zn;
If (Mr [ZM] = nR [Zn])
{T = T * Mr [ZM]; ZM ++; Zn ++ ;}
}

Return T;
}

*************** *********/
Int my_sieve (unsigned int N, unsigned int * l)
{
Int I;
Unsigned int P, J, a [Max];

For (P = 2; P <= N; ++ P)
A [p] = P;
For (P = 2; p * P <= N; ++ P)
{
If (A [p]! = 0)
{
J = p * P;
While (j <= N)
{
A [J] = 0;
J + = P;
}
}/* End if */
}/* End */

I = 0;
For (P = 2; P <= N; ++ P)
{
If (A [p]! = 0)
{
L [I] = A [p];
++ I;
}
}

Return I;
}

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.