[Algorithm] - prime 筛法

Erichsen sieve method

Definition of primes: prime number is in addition to 1 and itself has no other divisors, it is not about the number of primes.
The main idea Erichsen sieve method is to remove all multiples of primes Figure:
Here Insert Picture Description
This image contains the main idea sieve of
the following codes to realize pair of

#include <stdio.h>
#include <string.h>//memset函数的头文件

int l[10005];//用于标记数字
int p[10005], num = 0;//用于储存素数,记录素数个数
int find_prime(int n)
{
    for(int i = 2; i <= n; i++)
    {
        if(!l[i])//0为素数,1为合数
        {
            p[num++] = i;
            for(int j = i*2; j <= n; j += i)//排数素数的倍数
            {
                l[j] = 1;
            }
        }
    }
    return num;
}
int main(void)
{
    int n, ans;
    scanf("%d", &n);
    memset(l, 0, sizeof(l));//0为素数,1为合数
    memset(p, 0, sizeof(p));//清空l,p数组
    l[0] = 1;
    l[1] = 1;//排除特殊情况
    ans = find_prime(n);
    for(int i = 0; i < ans; i++)
    {
        printf("%d ", p[i]);//输出素数
    }
    printf("\n%d", ans);
    return 0;
}


The following is the output
Here Insert Picture Description

Euler's sieve method

Euler sieve time complexity is O (n), is also known as linear screen
main idea Euler sieve with sieve method same Eppendorf, Eppendorf sieve is optimized, we know that 30 = 15 = 2 * 3 * 10 = 5 * 6 will therefore result in a number was repeatedly drying out resulting in reduced efficiency, Euler sieve method is to make up for this deficiency
the main idea: arithmetic fundamental theorem: any natural number greater than 1, if n is not a prime number, then n can be uniquely decomposed into a finite number of qualitative product
with own words: composite number any number can be expressed as the product of the number of multiple factors, then a composite number must have a minimum quality factor, we just need to make this a minimum quality sieve to factor this number can, when we can not sift through is determined by determining the minimum quality factor, for example 30, is sieved prime number in ascending order, we need only use 2 to 30 mesh to , which means that if 30% 2 == 0, then prove that the number has been out of the loop can be directly screened out
below is the code demonstrates:

#include <stdio.h>
#include <string.h>//memset函数的头文件

int l[10005];//用于标记数字
int p[10005], num = 0;//用于储存素数,记录素数个数
int find_prime(int n)
{
    num = 0;
    for(int i = 2; i <= n; i++)
    {
        if(!l[i])//0为素数,1为合数
        {
            p[num++] = i;
        }
        for(int j = 0; j < num && p[j] * i <= n; j++)
        {
            l[p[j] * i] = 1;
            if(i % p[j] == 0) break;
        }
    }
    return num;
}
int main(void)
{
    int n, ans;
    scanf("%d", &n);
    memset(l, 0, sizeof(l));//0为素数,1为合数
    memset(p, 0, sizeof(p));//清空l,p数组
    l[0] = 1;
    l[1] = 1;//排除特殊情况
    ans = find_prime(n);
    for(int i = 0; i < ans; i++)
    {
        printf("%d ", p[i]);//输出素数
    }
    printf("\n%d", ans);
    return 0;
}

Output
Here Insert Picture Description

Published 20 original articles · won praise 2 · Views 940

Guess you like

Origin blog.csdn.net/zhbbbbbb/article/details/103529181