Created
March 24, 2013 13:45
-
-
Save killwing/5232026 to your computer and use it in GitHub Desktop.
[getPrime] kinds of primes generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn lazy-primes [] | |
(letfn [(enqueue [sieve n step] | |
(let [m (+ n step)] | |
(if (sieve m) | |
(recur sieve m step) | |
(assoc sieve m step)))) | |
(next-sieve [sieve candidate] | |
(if-let [step (sieve candidate)] | |
(-> sieve | |
(dissoc candidate) | |
(enqueue candidate step)) | |
(enqueue sieve candidate (+ candidate candidate)))) | |
(next-primes [sieve candidate] | |
(if (sieve candidate) | |
(recur (next-sieve sieve candidate) (+ candidate 2)) | |
(cons candidate | |
(lazy-seq (next-primes (next-sieve sieve candidate) | |
(+ candidate 2))))))] | |
(cons 2 (lazy-seq (next-primes {} 3))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
bool isPrime(unsigned int n, const vector<unsigned int>& list) { | |
if (n == 1) { | |
return false; | |
} | |
for (auto i : list) { | |
if (n % i == 0) { | |
return false; | |
} | |
if (i*i > n) { | |
break; | |
} | |
} | |
return true; | |
} | |
unsigned int getPrime(unsigned int n) { | |
vector<unsigned int> list; | |
unsigned int i = 1; | |
while (list.size() < n) { | |
if (isPrime(i, list)) { | |
list.push_back(i); | |
} | |
++i; | |
} | |
return --i; | |
} | |
int main() { | |
cout << getPrime(1000000) << endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def primes(): | |
candidates = itertools.count(2) | |
while True: | |
prime = next(candidates) | |
candidates = (lambda prime: (i for i in candidates if i % prime))(prime) | |
yield prime | |
print(list(itertools.islice(primes(),0,100))[100-1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prime' | |
puts Prime.lazy.take(1000000).force.last |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
vector<unsigned int> sievePrimes(unsigned int limit) { | |
vector<unsigned int> primes; | |
if (limit > 1) { | |
primes.push_back(2); | |
vector<bool> sieve((limit + 1) / 2, false); | |
for (unsigned long long i = 1, n = 3; i < sieve.size(); ++i, n += 2) { | |
if (!sieve[i]) { | |
primes.push_back(n); | |
for (unsigned long long j = n * n / 2; j < sieve.size(); j += n) { | |
sieve[j] = true; | |
} | |
} | |
} | |
} | |
return primes; | |
} | |
int main() { | |
vector<unsigned int> p = sievePrimes(16000000); | |
cout << p.size() << endl; | |
cout << p[1000000-1] << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment