Created
May 14, 2012 18:07
-
-
Save foota/2695402 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
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
| std::vector<int> primes; | |
| for (int i = 3; i < 100; i += 2) primes.push_back(i); | |
| auto end = primes.end(); | |
| for (auto x = primes.begin(); *x * *x <= *(end-1); ++x) | |
| end = std::remove_if(x + 1, end, [&x](int p){ return p % *x == 0; }); | |
| primes.erase(end, primes.end()); | |
| for (auto p = primes.begin(); p != primes.end(); ++p) std::cout << *p << " "; | |
| std::cout << std::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
| primes = sieve [2..] | |
| sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0] |
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
| primes = range(3, 100, 2) | |
| for i in range(len(primes)): | |
| if primes[i]**2 > primes[-1]: break | |
| primes[i+1:] = filter(lambda p: p % primes[i] != 0, primes[i+1:]) | |
| print primes | |
| """ | |
| python -c "import sys;globals().__setitem__('P',range(3,100,2));[P.__setslice__(i+1,len(P),filter(lambda p:p%P[i]!=0,P[i+1:]))for i in range(len(P))if i<len(P)and P[i]**2<=P[-1]];[sys.stdout.write('%d '%p)for p in P]" | |
| """ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://handasse.blogspot.com/2010/09/blog-post.html