Skip to content

Instantly share code, notes, and snippets.

@mudler
Created June 6, 2014 20:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mudler/6c7b7da4b2a07ece6ec3 to your computer and use it in GitHub Desktop.
Save mudler/6c7b7da4b2a07ece6ec3 to your computer and use it in GitHub Desktop.
prime numbers with vec in perl
#!/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);
}
@mudler
Copy link
Author

mudler commented Jun 6, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment