diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-03-06 21:58:35 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-03-06 21:58:35 +0800 |
| commit | 0f6201ecf5e4cca2a28f4da9c5fe161216d6e8b5 (patch) | |
| tree | 819d823cbfb1f76b755a0a200c936e1a7d0e9ef6 /challenge-154 | |
| parent | f7ba96ee875e8c2a1d2b630c15ab599e0af6a337 (diff) | |
| download | perlweeklychallenge-club-0f6201ecf5e4cca2a28f4da9c5fe161216d6e8b5.tar.gz perlweeklychallenge-club-0f6201ecf5e4cca2a28f4da9c5fe161216d6e8b5.tar.bz2 perlweeklychallenge-club-0f6201ecf5e4cca2a28f4da9c5fe161216d6e8b5.zip | |
Week 154
Diffstat (limited to 'challenge-154')
| -rw-r--r-- | challenge-154/cheok-yin-fung/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-154/cheok-yin-fung/perl/ch-2-test-pre_calc.pl | 52 | ||||
| -rw-r--r-- | challenge-154/cheok-yin-fung/perl/ch-2.pl | 6 |
3 files changed, 57 insertions, 3 deletions
diff --git a/challenge-154/cheok-yin-fung/perl/ch-1.pl b/challenge-154/cheok-yin-fung/perl/ch-1.pl index 76313f2e6f..dd94411614 100644 --- a/challenge-154/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-154/cheok-yin-fung/perl/ch-1.pl @@ -24,6 +24,7 @@ sub look_for { my @sw = split "", $word; + # all permutations my $iter = Algorithm::Permute->new(\@sw, length($word)); my @words; while (my @res = $iter->next) { @@ -31,6 +32,7 @@ sub look_for { } @words = uniqstr @words; + # use a hash to determine whether a permutation has appeared my %bucket = map {$_ => 0} @words; for (@collection) { diff --git a/challenge-154/cheok-yin-fung/perl/ch-2-test-pre_calc.pl b/challenge-154/cheok-yin-fung/perl/ch-2-test-pre_calc.pl new file mode 100644 index 0000000000..3ff75bbecc --- /dev/null +++ b/challenge-154/cheok-yin-fung/perl/ch-2-test-pre_calc.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl +# The Weekly Challenge 154 +# Task 2 Padovan Prime + +use v5.22.0; +use warnings; +use Math::BigInt; + +use Math::Prime::Util qw/prime_precalc is_prime/; +prime_precalc(10_000_000); + + + +my $NUM_OF_TERMS = $ARGV[0] || 10; + +my @ans = (); +my $pdvstate = [Math::BigInt->new(1), Math::BigInt->new(1), + Math::BigInt->new(1), Math::BigInt->new(2)]; + +core(); + + + +sub pdv_iter { + my ($a, $b, $c, $d) = @{$pdvstate}; + ($a, $b, $c, $d) = ($b, $c, $d, $b+$c); + @$pdvstate = ($a, $b, $c, $d); + return $c; +} + + + +sub core { + my $ind = 3; # the index is set to be consistent with OEIS:A112882 + while (scalar @ans < $NUM_OF_TERMS) { + my $Pk = pdv_iter(); + if (is_prime($Pk)) { + update_ans($Pk); + } + $ind++; + } +} + + + +sub update_ans { + my $candidate = $_[0]; + push @ans, $candidate if defined($ans[-1]) + && $candidate != $ans[-1]; + push @ans, $candidate if !defined($ans[-1]); + return $ans[-1]; +} diff --git a/challenge-154/cheok-yin-fung/perl/ch-2.pl b/challenge-154/cheok-yin-fung/perl/ch-2.pl index 8ebe5d7ae5..9783f4ef4b 100644 --- a/challenge-154/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-154/cheok-yin-fung/perl/ch-2.pl @@ -6,8 +6,8 @@ use v5.22.0; use warnings; use Math::BigInt; -use Math::Prime::Util ':all'; -prime_precalc(10_000_000); +use Math::Prime::Util qw/prime_precalc is_prime/; +prime_precalc(100_000_000); @@ -34,7 +34,7 @@ sub core { my $ind = 3; # the index is set to be consistent with OEIS:A112882 while (scalar @ans < $NUM_OF_TERMS) { my $Pk = pdv_iter(); - if (is_prob_prime($Pk)) { + if (is_prime($Pk)) { say update_ans($Pk); } $ind++; |
