This commit is contained in:
Zengtudor 2024-12-29 10:37:23 +08:00
parent c6e88be7b1
commit 9400bf480d

49
src/12/c14/c14_prime.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <cstddef>
#include <iostream>
#include <ostream>
#include <type_traits>
#include <vector>
using namespace std;
template<class T>
ostream&operator<<(ostream &os,vector<T> const& v){
os<<"{ ";
if(v.size()==0){
os<<" }";
}else{
for(size_t i=0;i<v.size()-1;i++){
os<<v[i]<<" ,";
}
os<<v[v.size()-1]<<" }";
}
return os;
}
vector<int> getPrimes(int const& end){
vector<bool> isFactor(end+1,false);
for(int i=2;i<end;i++){
if(isFactor[i]==false){
for(int j=i+i;j<end;j+=i){
isFactor[j]=true;
}
}
}
vector<int> primes;
for(int i=2;i<end;i++){
if(isFactor[i]==false){
// cout<<i<<' ';
primes.push_back(i);
}
}
return primes;
}
int main(){
// const auto primes = getPrimeNum(101);
// for(int const& i:primes){
// cout<<i<<' ';
// }
// cout<<'\n';
cout<<"primes nums: "<<getPrimes(100+1)<<'\n';
}