Chủ Nhật, 10 tháng 4, 2022

PRIMET C++


#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e9 + 3;
const int N = 1e5 + 3;
int n;
bool p[N];

void sang()
{
    memset(p, true, sizeof p);
    p[1] = false;
    for (int i = 2; i * i <= 1e5; i++)
        if (p[i])
        {
            for (int j = i * i; j <= 1e5; j += i)
                p[j] = false;
        }
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
   // freopen("Prime.inp", "r", stdin);
   // freopen ("Prime.out", "w", stdout);
    sang();
    cin >> n;
    int res = 0;
    for (int i = 2; i*i <= n; i++)
    {
        //if (i * i >= n) break;
        if (p[i]) res++;
    }
    cout << res;
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.