Skip to content

Commit

Permalink
半素数を列挙する
Browse files Browse the repository at this point in the history
  • Loading branch information
sugyan committed May 12, 2009
1 parent 9a3cc5f commit 21f8016
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lang/perl/semiprime/math_big_factors.pl
@@ -0,0 +1,10 @@
#!/opt/local/bin/perl
use strict;
use warnings;

use Math::Big::Factors qw(factors_wheel);

for my $num (1..99) {
my @factors = factors_wheel($num);
print "$num\n" if scalar(@factors) == 2;
}
27 changes: 27 additions & 0 deletions lang/perl/semiprime/no_module.pl
@@ -0,0 +1,27 @@
#!/usr/bin/perl
use strict;
use warnings;

# 最小の素因数を返すサブルーチン
# 引数が素数だった場合はundefを返す
sub get_factor {
my $target = shift;
for my $num (2 .. $target-1) {
if ($target % $num == 0) {
return $num;
}
}
return;
}

TARGET: for my $num (1..99) {
my $first_factor = get_factor($num);
if (!defined($first_factor)) {
next TARGET;
}

my $second_factor = get_factor($num / $first_factor);
if (!defined($second_factor)) {
print "$num\n";
}
}

0 comments on commit 21f8016

Please sign in to comment.