diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-03-19 00:42:02 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-03-19 00:42:02 +0800 |
| commit | 6949f7b6c244ef95a0a3fa1f9d916ac8133831ef (patch) | |
| tree | 2c37d0038e3dde44584d9f0b38e2212f6dceb55a /challenge-156 | |
| parent | 8b784c4f595a470f4b01639fe4bbd1cadfd3cb36 (diff) | |
| download | perlweeklychallenge-club-6949f7b6c244ef95a0a3fa1f9d916ac8133831ef.tar.gz perlweeklychallenge-club-6949f7b6c244ef95a0a3fa1f9d916ac8133831ef.tar.bz2 perlweeklychallenge-club-6949f7b6c244ef95a0a3fa1f9d916ac8133831ef.zip | |
Task 1
Diffstat (limited to 'challenge-156')
| -rw-r--r-- | challenge-156/cheok-yin-fung/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-156/cheok-yin-fung/perl/ch-1.pl | 37 |
2 files changed, 24 insertions, 14 deletions
diff --git a/challenge-156/cheok-yin-fung/blog.txt b/challenge-156/cheok-yin-fung/blog.txt new file mode 100644 index 0000000000..96fd21704b --- /dev/null +++ b/challenge-156/cheok-yin-fung/blog.txt @@ -0,0 +1 @@ +https://E7-87-83.github.io/coding/challenge_156.html diff --git a/challenge-156/cheok-yin-fung/perl/ch-1.pl b/challenge-156/cheok-yin-fung/perl/ch-1.pl index 82b1555505..bbf0bbfbf8 100644 --- a/challenge-156/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-156/cheok-yin-fung/perl/ch-1.pl @@ -1,27 +1,36 @@ #!/usr/bin/perl use v5.22.0; use warnings; -use Math::Prime::Util qw 'is_prime'; +use Math::Prime::Util qw /next_prime/; +use Algorithm::Combinatorics qw/combinations/; my $N = $ARGV[0] || 10; -my $count = 0; +my $list_size = $N; +my $ub = 3*$N; -for (1..3*$N) { - if (pernicious_simple($_)) { - say $_; - $count++; +my @pern_num = (); + +for my $k (2..$ub) { + my $prime = 2; + while ($prime <= $k) { + my @weight_k_pern_num = (); + my $iter = combinations([1..$k-1], $prime-1); + while (my $c = $iter->next) { + my @ch = ((1), (0) x ($k-1)); + $ch[$_] = 1 for @{$c}; + my $new_pern_num = oct("0b".(join "", @ch)); + push @weight_k_pern_num, $new_pern_num; + } + push @pern_num, @weight_k_pern_num; + $prime = next_prime($prime); } - last if $count == $N; + last if scalar @pern_num >= $N; } -die "bound too small\n" if $count < $N; + +@pern_num = sort {$a<=>$b} @pern_num; +say join ", ", @pern_num[0..$N-1]; -sub pernicious_simple { - my $num = $_[0]; - my $count_one = scalar - grep { $_ == 1 } split "", sprintf("%b",$num); - return is_prime($count_one) ? 1 : 0; -} |
