The C language implements prime numbers, least common multiples, maximum common approx., number of input, prime number, number of completion, and daffodils algorithm.

Source: Internet
Author: User

1. Algorithms for prime numbers:

Two for loops are used. The outer loop is the required range. The inner loop ranges from 2 to SQRT (a value of + 1 in the outer loop range). A flag is set to indicate whether it is a prime number;

Specific implementation:

# Include <stdio. h>

# Include <math. h>

Int main (void)

{

Int I, J;

Bool flag = 0; // set a flag. 0 indicates a non-prime number, and 1 indicates a prime number;

For (I = 101; I <= 200; I ++) // calculate 101 ~ The prime number of 200, which is printed out;

{

For (j = 2; j <= SQRT (double) (I + 1); j ++)

{

If (I % J = 0)

{

Flag = 0;

Break;

}

Else

Flag = 1;

}

If (FLAG)

Printf ("% d", I );

}

Return 0;

}

2. Algorithms for daffodils:

It is mainly to know the concept of the number of daffodils, and then separate the single digit, ten digits and hundreds of digits;

The so-called "Daffodils" refers to a three-digit number, each digit of which is equal to the number itself.

Specific implementation:

# Include <stdio. h>

Int main (void)

{

Int I;

Int Weiwei, Shiwei, gewei;

For (I = 100; I <1000; I ++)

{

Int temp = 0;

Gewei = I % 10; // separate a bit;

Shiwei = I/10% 10; // separated by ten;

Wei = I/100; // separates hundreds of digits;

Temp = gewei * gewei + Shiwei * Shiwei + Wei * Wei;

If (I = temp)

Printf ("I = % d \ n", I );

}

Return 0;

}

3. algorithms for the maximum public approx. And the minimum public multiples:

The maximum common divisor must be the largest of the common divisor, that is, the last one in the common divisor;

The minimum public multiple must be the smallest of the Public multiples, that is, the first starting public multiple.

Specific implementation:

# Include <stdio. h>

Int main (void)

{

Int m, n, I, j;

Int GCD, LCM;

Printf ("Please input two integer \ n ");

Scanf ("% d", & M, & N );

For (I = 1; I <(M> n? M: N); I ++)

{

If (M % I = 0 & N % I = 0)

GCD = I; // obtain the last approximate number;

}

For (j = (M> n? M: N); j <= m * n; j ++)

{

If (J % m = 0 & J % N = 0)

{

LCM = J;

Break; // immediately jumps out of the loop when a public multiple is obtained;

}

}

Printf ("greatest common divisor is % d \ n", gcd );

Printf ("least common multiple is % d \ n", LCM );

Return 0;

}

4. Number of algorithms:

Definition: final number, that is, perfect number. If a number is equal to the sum of factors other than itself, this number is called final number.

Specific implementation:

# Include <stdio. h>

Int main (void)

{

Int I, j, R;

For (I = 1; I <= 1000; I ++)

{

R = 0;

For (j = 1; j <I; j ++)

{

If (I % J = 0) // This sentence is critical;

R = R + J;

}

If (r = I)

Printf ("perfect number is % d \ n", R );

}

Return 0;

}

5. The number of retrieval algorithms:

Definition: "Number of input" is a number. For example, if the number is 98789, the number is 98789, and the number is 98789. The number is the same as the number of reading.

Algorithm Implementation:

# Include <stdio. h>

# Include <string. h>

Int main (void)

{

Char STR [100];

Char * pA, * pb; // Use Pointer;

Printf ("Please input a string \ n ");

While (scanf ("% s", STR )! = EOF)

{

Pa = STR; // point to the first character;

PB = STR + strlen (STR)-1; // point to the last non-(\ 0) character;

While (* pA = * pb & Pa <Pb)

{

Pa ++;

Pb --;

}

If (Pa> = Pb) // indicates the number of input files;

Printf ("True \ n ");

Else

Printf ("false \ n ");

}

Return 0;

}

6. Prime Number algorithms:

Definition: prime number is a number that can only be divided by 1 and itself, such as 2, 3, 5, 7, 11 ......

Algorithm Implementation:

# Include <stdio. h>

Int main (void)

{

Int I, J;

Bool flag = true; // set the flag. The default value is true;

For (I = 2; I <= 100; I ++)

{

For (j = 2; j <I; j ++)

{

If (I % J! = 0)

{

Flag = true; // if it cannot be divisible, the flag is set to true;

}

Else

{

Flag = false; // if it can be divisible, set the flag to false and exit the loop;

Break;

}

}

If (flag = true) // when the flag is true, a number is printed;

Printf ("% d", I );

}

Return 0;

}

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.