diff options
| -rwxr-xr-x | challenge-147/wlmb/perl/ch-1.pl | 2 | ||||
| -rwxr-xr-x | challenge-147/wlmb/perl/ch-2.pl | 7 | ||||
| -rwxr-xr-x | challenge-147/wlmb/perl/ch-2a.pl | 3 | ||||
| -rwxr-xr-x | challenge-147/wlmb/perl/ch-2b.pl | 1 |
4 files changed, 7 insertions, 6 deletions
diff --git a/challenge-147/wlmb/perl/ch-1.pl b/challenge-147/wlmb/perl/ch-1.pl index 1fa2fbebe8..b2842052a4 100755 --- a/challenge-147/wlmb/perl/ch-1.pl +++ b/challenge-147/wlmb/perl/ch-1.pl @@ -7,7 +7,7 @@ use v5.12; use warnings; use PDL; use PDL::NiceSlice; -use POSIX qw(); # don't import to avoid name collisions with PDL +use POSIX (); # don't import to avoid name collisions with PDL use Text::Wrap qw(wrap $columns $break); die "Usage: ./ch-1.pl size_of_sieve number_of_truncatable_primes [base]\n" diff --git a/challenge-147/wlmb/perl/ch-2.pl b/challenge-147/wlmb/perl/ch-2.pl index 88eecdda76..f2edfb1a85 100755 --- a/challenge-147/wlmb/perl/ch-2.pl +++ b/challenge-147/wlmb/perl/ch-2.pl @@ -5,12 +5,13 @@ # See https://wlmb.github.io/2022/01/10/PWC147/#task-2-pentagon-numbers use v5.12; use warnings; -use bigint; +use POSIX qw(floor); use Time::HiRes qw(time); die "Usage: ./ch-2.pl largest_index\n" unless @ARGV==1; my $N=shift; my $start=time(); +use integer; J: foreach my $j(2..$N){ my $p=$j*(3*$j-1)/2; @@ -21,10 +22,12 @@ J: last J if pentagonal($q+$p) && pentagonal($p-$q); } } +no integer; # don't truncate time say "Time: ", time()-$start; +use integer; sub pentagonal { my $p=24*shift()+1; - my $s=sqrt($p); + my $s=floor(sqrt($p)); return $s**2==$p && $s%6==5; } sub index_of { diff --git a/challenge-147/wlmb/perl/ch-2a.pl b/challenge-147/wlmb/perl/ch-2a.pl index 038b9630d3..5dca4ebda9 100755 --- a/challenge-147/wlmb/perl/ch-2a.pl +++ b/challenge-147/wlmb/perl/ch-2a.pl @@ -14,8 +14,7 @@ my $N=shift; my $start=time(); my $n=zeroes(long, $N)->xvals+1; my $p=$n*(3*$n-1)/2; -my $check=pentagonal($p); -for my $i (2..$p->nelem){ +for my $i (1..$p->nelem){ my $pi=$p(($i-1)); my $pass=which(pentagonal($pi+$p) & pentagonal($pi-$p)); next unless $pass->nelem; diff --git a/challenge-147/wlmb/perl/ch-2b.pl b/challenge-147/wlmb/perl/ch-2b.pl index c029154bc9..320dfedddc 100755 --- a/challenge-147/wlmb/perl/ch-2b.pl +++ b/challenge-147/wlmb/perl/ch-2b.pl @@ -14,7 +14,6 @@ my $N=shift; my $start=time(); my $n=zeroes(long, $N)->xvals+1; my $p=$n*(3*$n-1)/2; -my $check=pentagonal($p); my $pass=whichND(pentagonal($p+$p(*1)) & pentagonal($p-$p(*1))); die "No solution found. Try to increase the largest_index" unless $pass->dim(1)>0; my $ij=$pass(:,(0))+1; |
