Skip to content

Instantly share code, notes, and snippets.

@jadiunr
Created June 29, 2019 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadiunr/cd9bc184d518dee775baa24ab61d5fc3 to your computer and use it in GitHub Desktop.
Save jadiunr/cd9bc184d518dee775baa24ab61d5fc3 to your computer and use it in GitHub Desktop.
エラトステネスの篩
use strict;
use warnings;
my $n = $ARGV[0] || 1000;
my $nums = [2..$n];
my $p_index = 0;
while (1) {
my $p = $nums->[$p_index++];
last if $p > sqrt($n);
@$nums = grep { ($_ == $p) or ($_ % $p) } @$nums;
}
$" = "\n";
print "1\n@$nums";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment