Line sieve to find prime numbers

1. The definition of prime numbers

1. A natural number that can only be divisible by 1 and itself is called a prime number. It is specifically stipulated that 1 is not a prime number.

According to the definition of prime number, it is obvious that if a number is prime <==> its factor only contains 1 and itself. Therefore, it can be judged whether it is a prime number according to the method of judging the factor of a certain number.

int isprime(int n)
{
    
    
    int i;
    for(i=2;i<=(int)sqrt((double)n);i++)
    {
    
    
        if(n%i==0)    //如果n存在其它因子,则必定不是素数
         {
    
    
            return 0;
        }
    }
    return 1;
}

But if it is required to find all prime numbers within 1,000,000, the efficiency of the above method is very low, so the screening method is usually used to find prime numbers. Screening method: For a number n, if it is a prime number, then 2 n, 3 n, 4*n, must not be a prime number.

bool isprime[1000001];
int prime[80000];
int num=0;
void getPrime()     //用筛选法求算素数
{
    
    
    int i,j;
    for(i=0;i<1000001;i++)
    {
    
    
        isprime[i]=true;
    }
    for(i=2;i<=1000;i++)        //如果isprime[i]==true,即i是素数,那么i,2*i,3*i必定不是素数
     {
    
    
        for(j=i+i;j<=1000000;j+=i)
        {
    
    
            if(isprime[i]==true)
                isprime[j]=false;
        }
    }
    for(i=2;i<1000001;i++)
    {
    
    
        if(isprime[i]==true)
        {
    
    
            prime[num++]=i;
        }
    }
}

The screening method has a very general algorithm for finding prime numbers. For example, to check whether a number N is prime or not, divide N by the root of 2-N. As long as there is one that can be divisible, it means that N is not a prime number. In addition, this question requires an array to be calculated.

2. Line screen

The so-called "sieving method" refers to the "Eratosthenes sieve method". He is a famous mathematician in ancient Greece. The method he adopted is to write all the integers from 1 to 100 on a piece of paper, and then determine whether they are prime numbers one by one, and find a non-prime number, then dig it out, and what remains is the prime number.

The specific methods are as follows:

First dig out 1 (because 1 is not a prime number).
Divide the numbers after it with 2, and dig out the numbers that are divisible by 2, that is, dig out multiples of 2.
Divide the numbers after it with 3, and dig out multiples of 3.
Divide the numbers after these numbers by 4, 5..., respectively. This process continues until all the numbers after the divisor have been mined. For example, to find a prime number from 1 to 50, you must continue until the divisor is 47 (in fact, it can be simplified. If you need to find a table of prime numbers in the range of 1 to n, you only need to proceed until the divisor is n 2 ( radius n), whichever is An integer is sufficient. For example, for 1 to 50, you only need to proceed to 50 2 as the divisor.) The
above algorithm can be expressed as:

Dig out 1;
use the next number p of the number dug out just now to divide the numbers after p, and dig out the multiples of
p ; check whether p is less than the integer part of n^2 (if n=1000, then check p<31 ?), if it is, return to (2) to continue execution, otherwise it ends; the
remaining numbers on the paper are prime numbers.

#include <stdio.h>
#include <math.h>
int main(void)
{
    
    
    int i;
    int j;
    int a[101];                // 为直观表示,各元素与下标对应,0号元素不用
    for (i = 1; i <= 100; i++) // 数组各元素赋值
        a[i] = i;
    for (i = 2; i < sqrt(100); i++)     // 外循环使i作为除数
        for (j = i + 1; j <= 100; j++)  // 内循环检测除数i之后的数是否为i的倍数
        {
    
    
            if (a[i] != 0 && a[j] != 0) // 排除0值元素
                if (a[j] % a[i] == 0)
                    a[j] = 0;           // i后数若为i的倍数,刚将其置0(挖去)
        }
    int n = 0;    // 对输出素数计数, 以控制换行显示
    for (i = 2; i <= 100; i++)    // 输出素数
    {
    
    
        if (a[i] != 0)
        {
    
    
            printf("%-5d", a[i]); // 输出数组中非0元素(未挖去的数)
            n++;
        }
        if (n == 10)
        {
    
    
            printf("\n");         // 每行10个输出
            n = 0;
        }
    }
    printf("\n");
    return 0;
}

Three, practice questions

Prime number

Guess you like

Origin blog.csdn.net/qq_32431299/article/details/112433549