LoginSignup
1
0

More than 5 years have passed since last update.

合成数列の和アドベントカレンダー Perl編

Last updated at Posted at 2018-12-16

合成数列の和アドベントカレンダー Advent Calendar 2018 の 16日目です。

ルール

  • 入力として正の整数 N を与えたら 4 から始まる 合成数 の数列の 1 番目から N 番目からの合計を出力してください
  • N は最大で 100 とします
test.pl
#!/usr/local/bin/perl
use strict;
use warnings;

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

my $sum = 0;
my $sum_count = 0;
my $num = 4;
while ( 1 ) {
    my @factors = factors_wheel($num);
    if (defined($factors[1])) {
        $sum += $num;
        $sum_count++;
        last if $sum_count == $ARGV[0];
    }
    $num++;
}
print "$sum\n";

※Math::Big::Factorsを使用

出力例

$ perl test.pl 2
10
$ perl test.pl 4
27
$ perl test.pl 10
112
$ perl test.pl 100
7059
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0