1. 程式人生 > >【素數】Eratosthenes篩選

【素數】Eratosthenes篩選

bit sin pac memset ++ cin nes mem bsp

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int main()
{
    /*素數篩選*/
    /*Eratosthenes篩選*/
    /*確定某一個數是素數之後,刪除這個數的所有的倍數*/
    int n;
    cin >> n;
    memset(vis, 0, sizeof(vis));
    for(int i = 2; i <= sqrt(n); i++)
        
if(!vis[i]) for(int j = i * i; j <= n; j += i) vis[j] = 1; }

【素數】Eratosthenes篩選