Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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