#include <iostream> #include <limits.h> using namespace std; void sieve(int n) { if(n <= 1) return; bool isPrime[n+1]; fill(isPrime, isPrime + n + 1, true); for(int i=2; i*i <= n; i++) { if(isPrime[i]) { for(int j = 2*i; j <= n; j = j+i) { isPrime[j] = false; } } } for(int i = 2; i<=n; i++) { if(isPrime[i]) cout<<i<<" "; } } int main() { int n = 18; sieve(n); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter