diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-12-24 01:01:14 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-24 01:01:14 +0000 |
| commit | 776396430ae1e9e0aa85d2e78bb92e38fd1556f7 (patch) | |
| tree | 6e8d0ac1bc1438f1e0cd4a7ac609dda25dd66bf0 | |
| parent | b369a8ed20a9d70d0d2500c5bd693b8be678f2c1 (diff) | |
| parent | c715c9c6a28663983f51848c96699ffb23e6189f (diff) | |
| download | perlweeklychallenge-club-776396430ae1e9e0aa85d2e78bb92e38fd1556f7.tar.gz perlweeklychallenge-club-776396430ae1e9e0aa85d2e78bb92e38fd1556f7.tar.bz2 perlweeklychallenge-club-776396430ae1e9e0aa85d2e78bb92e38fd1556f7.zip | |
Merge pull request #5408 from wlmb/challenges
Challenges
| -rwxr-xr-x | challenge-144/wlmb/perl/ch-2.pl | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/challenge-144/wlmb/perl/ch-2.pl b/challenge-144/wlmb/perl/ch-2.pl index cb1e094603..0a2b93e95a 100755 --- a/challenge-144/wlmb/perl/ch-2.pl +++ b/challenge-144/wlmb/perl/ch-2.pl @@ -10,18 +10,17 @@ use PDL::NiceSlice; say("Usage: ./ch-2.pl u v [N]\nto find the first N (default 10) terms". " of the Ulam sequence u,v..."),exit unless @ARGV==2 || @ARGV==3; say("The given numbers should not be equal"), exit unless $ARGV[0]!=$ARGV[1]; -my $ulam=pdl(@ARGV[(0,1)]); # initialize sequence +my $ulam=pdl[$ARGV[0]]; # initialize sequence +my $candidates=pdl[$ARGV[1]]; # candidate list my $N=$ARGV[2]//10; -foreach(3..$N){ - my $sums=$ulam+$ulam->slice("*"); # addition table for Ulam sequence - my $ordered=$sums->where( - ($sums->xvals>$sums->yvals) # upper triangle in addition table - &($sums>$ulam((-1)) # remove previous terms - ))->qsort; # order - $ordered=$ordered->where( # eliminate duplicates with right or left - ($ordered!=$ordered->rotate(-1)) - &($ordered!=$ordered->rotate(1))) if $ordered->nelem>1; - $ulam=$ulam->append($ordered((0))); +foreach(2..$N){ + my $sl=$candidates->qsort; # short list + # remove duplicates + $sl=$sl->where(($sl!=$sl->rotate(1))&($sl!=$sl->rotate(-1))) if $sl->nelem>1; + my $next=$sl->((0)); # Next Ulam number + $candidates=$candidates->append($ulam+$next); # Update candidate list + $candidates=$candidates->where($candidates>$next); # remove those too small + $ulam=$ulam->append([$next]); # update list of ulam numbers } say "Input: u=$ARGV[0], v=$ARGV[1]", defined $ARGV[2]?", N=$N":""; say "Output: $ulam"; |
