Editorial for Đếm số nguyên tố từ 1 đến N


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
#include <bits/stdc++.h>
using namespace std;
int prime(int n){
    if(n < 2){
        return 0; // loại số âm, và 0, 1
    }
    for(int i = 2; i <= sqrt(n); i++){
        if(n % i == 0){
            return 0; // kết thúc ngay tại đây
        }
    }
    return 1; // là số nguyên tố
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    int dem = 0;
    cin>>n;
    for(int i = 1; i <= n; i++){
        if(prime(i) == 1){
            //printf("%d ", i);
            ++dem;
        }
    }
    cout<<dem;
    return 0;
}

</pre>

Comments

Please read the guidelines before commenting.


There are no comments at the moment.