Num 19: Greatest common divisor. least common multiple. Judgment of Prime number

Source: Internet
Author: User
Tags greatest common divisor



in C language learning, often encounter:

calculate greatest common divisor, least common multiple and prime number judgment problem;

To summarize here:


1. Greatest common divisor and least common multiple:


By mathematical knowledge we know:

least common multiple of two numbers = greatest common divisor of the product of these two numbers/two numbers;

So the problem of seeking greatest common divisor and least common multiple is actually a kind of problem;


①. Least common multiple:


Method One:

If x>y, from X, determine whether x can be divisible by y (x%y==0);

If divisible, x is both least common multiple;

If not divisible, calculate whether the 2x,3x,......nx can be divisible;

Implementation code:

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <math.h> #include <string.h>int lcm (int x,int y) {int temp;if (y>x) { Temp=x;x=y;y=temp;} Temp=x;while (x%y!=0) X+=temp;return x;} int main () {int a,b,num;while (scanf ("%d%d", &a,&b)!=eof) {printf ("%d\n", (LCM (b)));} return 0;} </span>

Method Two:

Calculate the least common multiple indirectly by calculating the greatest common divisor:

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <math.h> #include <string.h>int gcd (int x,int y) {int num=0;num= (x> Y?Y:X); while ((x%num!=0| | y%num!=0) &&num!=1) Num--;return num;} int main () {int a,b;while (scanf ("%d%d", &a,&b)!=eof) {printf ("%d\n", (a*b)/GCD (A, b));} return 0;} </span>

②. Greatest common divisor:


Method One:

(X<y) n determines whether or not X, Y is divisible by n at the same time.

If can x for greatest common divisor, otherwise judge n--;

See least common multiple code;


Method Two:

The Euclidean algorithm is simulated in a recursive way:

That is: X is the same as x%y (not 0) greatest common divisor;

Implementation code:

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <math.h> #include <string.h>int gcd (int a,int b) {   if (b==0) return A;   return gcd (B,A%B);//No need to judge A, b size, has been automatically judged;} int main () {int a,b;while (scanf ("%d%d", &a,&b)!=eof) {printf ("%d\n", (GCD (b)));} return 0;} </span>

second, the prime number of the judgment:

Method One:

Judging one by one, from 2 to sqrt (x) One by one, x can be divisible;

If possible, for composite; otherwise, for prime numbers;

Implementation code:

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <math.h>int sushu (int x) {for (int i=2;i<=sqrt (x); i++) if (x%i==0) return 0 ; return 1;} int main () {int n;while (scanf ("%d", &n)!=eof) if (Sushu (n)) printf ("yes\n"), Else printf ("no\n"); return 0;} </span>

Method Two:

The prime number plays the table judgment;

Create an array (only 0, 1 in the array) [1 for composite, 0 for prime number];

Starting from 2, backwards, all multiples of 2 must be composite, marked as 1;

Starting from 3, repeat the process, if some have been marked as composite, then skip;

Implementation code:

<span style= "FONT-SIZE:14PX;" > #include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h>int sushu[ 2000001];void sheet () {sushu[1]=0;sushu[2]=0;for (int i=2;i<=2000000;i++)        if (!sushu[i]) for        (int j=i+i;j <=2000000;j+=i)        sushu[j]=1;} int main () {    int n;memset (sushu,0,sizeof (Sushu));    Sheet ();    while (scanf ("%d", &n), N)    {        if (!sushu[n]) printf ("yes\n");        else printf ("no\n");    }    return 0;} </span>


All of the above are the basis of C language, only grasp the good foundation, can solve more profound problems!


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Num 19: Greatest common divisor. least common multiple. Judgment of Prime number

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.