Created
June 6, 2014 20:55
-
-
Save mudler/6c7b7da4b2a07ece6ec3 to your computer and use it in GitHub Desktop.
prime numbers with vec in perl
This file contains 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
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use constant MAX => 100; | |
my $sieve = ''; | |
vec($sieve, MAX, 1) = 0; | |
$sieve = ~$sieve; | |
for (my $i = 2; $i <= sqrt(MAX); $i++) { | |
next unless (vec($sieve, $i, 1)); | |
for (my $x = $i * 2; $x <= MAX; $x += $i) { | |
vec($sieve, $x, 1) = 0; | |
} | |
} | |
for (2..MAX) { | |
printf "%d\n",$_ if (vec $sieve, $_, 1); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from http://www.devshed.com/c/a/perl/more-perl-bits/
just modified output