素数筛——Eratosthenes 素数筛、Euler 素数筛
2017-06-13 13:59
507 查看
/***************************************************** > File Name: liner_filter.cpp > Author: Yuji CAO > Mail: 1007171627@qq.com > Created Time: 二 6/13 12:11:11 2017 ****************************************************/ #include<map> #include<vector> #include<unordered_set> #include<unordered_map> #include<list> #include<queue> #include<deque> #include<stack> #include<iostream> #include<set> using namespace std; /** * Eratosthenes 素数筛 **/ int prime_nlogn(int n) { vector<int> prime; unordered_set<int> checkout; for (int i = 2; i <= n; ++i) { if (!checkout.count(i)) prime.push_back(i); for (int j = i * 2; j <= n; j += i) { checkout.insert(j); } } return prime.size(); } /** * Euler 素数筛 **/ int prime_n(int n) { vector<int> prime; unordered_set<int> checkout; for (int i = 2; i <= n; ++i) { if (!checkout.count(i)) prime.push_back(i); for (int j = 0; j < prime.size(); ++j) { if (prime[j] * i > n) break; checkout.insert(prime[j] * i); if (i % prime[j] == 0) break; } } return (int) prime.size(); } int main() { while (true) { int n; cin>>n; int a = prime_n(n); int b = prime_nlogn(n); cout<<a<<":"<<b<<endl; } return 0; }
相关文章推荐
- 素数表(Eratosthenes)
- 用Eratosthenes筛子算法,求解从2到n之间的素数
- 素数表(Eratosthenes)
- Eratosthenes筛选 求素数
- 《程序员密码学》之大数算数&Eratosthenes素数筛选
- The Sieve of Eratosthenes (素数筛选法)
- 素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】
- Eratosthenes 素数表生成方法
- 著名的Eratosthenes求素数方法(程序员的数学思维修炼)
- Eratosthenes筛法求素数
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
- 著名的Eratosthenes求素数方法(程序员的数学思维修炼)
- Eratosthenes 筛选素数
- Sieve of Eratosthenes(埃拉托斯特尼素数筛选法)--java实现
- Eratosthenes筛法求1-100之间的素数
- 用 Eratosthenes筛法构造1-n的素数表.
- Eratosthenes 找素数
- 查找素数的Eratosthenes(筛子)算法终止条件解释
- 数论之素数,包括eratosthenes算法,欧拉函数
- Eratosthenes 法找素数 (含代码)