The optimization of C language for prime numbers

Source: Internet
Author: User
Tags square root

Beginner C language, see the code of the prime number, not too satisfied, their own design a calculation flowchart. In the bar also asked, and "Erato (Eratosthenes) Sieve method" Some crashes, of course, in the next dare not to compete with the math masters of the length, but still want to try it yourself.

Baidu a bit someone has already used this kind of method to make the code, but with my idea discrepancy.

Ideas:

1 to avoid redundant calculation of the calculation times, so that when the number is large enough to reflect the higher computational efficiency,

1) The denominator excludes even numbers, which can be reduced by half. The specific practice is to change +1 increments from the second Prime number 3 to +2 increments;

2) filter the molecules to calculate the remainder, and according to the definition of the prime number, as long as not be less than the denominator of the prime number divisible,? Set the maximum residual molecule to the denominator square root +1; This avoids the redundancy of each number and can reduce the amount of redundant computation.

3) Rapid elimination of composite, starting from the smallest prime number, can be faster and more efficient

2 to find a prime number, and then assign a value, you can avoid storing useless values, using the identified primes as a molecule to calculate, can further improve efficiency.

After 2 nights of groping, finally succeeded, (the beginner is like this, or do not ask too much ...)

Here's the code:

——————————————————————————

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>


int main ()
{


int a[1001];//Array
int n;//number of output primes required
int m;//calculate the middle number
printf ("Pro, how many primes do you want? Do not exceed 1000 oh. \ n");
scanf ("%d", &n);
a[0]=2;//assigns two special prime numbers to a number
a[1]=3;
int m1=1;//Loop 2 control variable

int M1;//M1: Maximum number of remainder molecules
Int J;
/*-------------------Loop body---------------------*/
for (int i=1;i<n;i++)//for1: To find the prime number from the smallest
{
M1=1;
for (M=A[I]+2;M1; m=m+2)//for2: Denominator +2 until the prime condition is met, the molecule
{
m1= (int) sqrt (M) +1;//calculates the largest molecule of the remainder operation
for (j=1;j<=i;j++)//for3: Starting from the smallest prime number to find the remainder
{
if (A[J]<M1)//Determine if the remainder molecule is large enough

{//If not large enough, the calculation can be divisible
if (m%a[j]==0)//If it is divisible

{
j=i+1;//urges For3 cycle to end

}
else//cannot be divisible.
{
}
}
Else
{
a[i+1]=m;//large enough to be assigned to the sequence number
J=i+1;//for3 Jump out

M1=0;//for2 Jump out
}
}
}
}
for (int iii=0;iii<n;iii++) {printf ("%7d,", A[III]);}

printf ("\ n");

Getch ();
return 0;

}

The optimization of C language for prime 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.