#include <iostream> #include <math.h> using namespace std; bool isprime(int num) { for (int i=2; i<=sqrt(num); i++) { if (num%i==0) { return false; break; } } return true; } int main() { int n; while(cin>>n) { for (int j=2; j<n-1; j++) if (isprime(j)) cout << j << " "; } return 0; }
#include <iostream> #include <cmath> #include <vector> using namespace std; bool isprime(int x) { if (x <= 1) return false; else if (x == 2) return true; for (int i = 2; i <= sqrt(x); ++i) { if (x % i == 0) return false; } return true; } vector<int> getAllPrimes(int n) { vector<int> res; if (n < 2) return res; for (int i = 2; i < n; ++i) { if (isprime(i)) res.push_back(i); } return res; } int main(void) { int n; cin >> n; vector<int> prms = getAllPrimes(n); for (auto p : prms) cout << p << " "; cout << endl; return 0; }
//质数 对 小于自己的质数 求余,不等于0;且小于自己的质数 的范围在[2,sqat(n)] #include <iostream> #include <vector> #include <cmath> using namespace std; vector<int> res; vector<int> CalP(int n) { if(n<2) return res; res.push_back(2); int i,j; for(i=3;i<=n;++i) { for(j=0;j<res.size()&&res[j]<=sqrt(i);++j) { if(i%res[j]==0) break; } if(j==res.size()||res[j]>sqrt(i)) res.push_back(i); } return res; } int main() { int n=100; CalP(n); for(int i=0;i<res.size();++i) cout<<res[i]<<endl; return 0; }
#include <iostream> #include <vector> using namespace std; int main(){ int n = 30; if(n <= 2){ cout<<"None."; return 0; } vector<int>res; res.push_back(2); int len = res.size(); int i = res[len-1] + 1; bool isonly; for(;i < n; ++i){ isonly = true; for(int j = 0; j < len; ++j){ if(res[j]*(i/res[j]) == i){ isonly = false; break; } } if(isonly == true){ res.push_back(i); len++; } } for(int k = 0; k < len; ++k){ cout<<res[k]<<" "; } }