Boustrophedon transform: Difference between revisions

Added Perl
m (→‎{{header|Raku}}: delete some superstitious brackets)
(Added Perl)
Line 145:
</pre>
 
=={{header|Perl}}==
Not really fulfilling the conditions of the stretch goal, but heading a little way down that path.
{{trans|Raku}}
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36; use experimental <builtin for_list>;
use ntheory <factorial lucasu nth_prime>;
use List::Util 'head';
use bigint;
 
sub abbr ($d) { my $l = length $d; $l < 41 ? $d : substr($d,0,20) . '..' . substr($d,-20) . " ($l digits)" }
sub sum (@a) { my $sum = Math::BigInt->bzero(); $sum += $_ for @a; $sum }
 
sub boustrophedon_transform (@seq) {
my @bt;
my @bx = $seq[0];
for (my $c = 0; $c < @seq; $c++) {
@bx = reverse map { sum head $_+1, $seq[$c], @bx } 0 .. $c;
push @bt, $bx[0];
}
@bt
}
 
my $upto = 100; #1000 way too slow
for my($name,$seq) (
'1 followed by 0\'s A000111', [1, (0) x $upto],
'All-1\'s A000667', [ (1) x $upto],
'(-1)^n A062162', [1, map { (-1)**$_ } 1..$upto],
'Primes A000747', [ map { nth_prime $_ } 1..$upto],
'Fibbonaccis A000744', [ map { lucasu(1, -1, $_) } 1..$upto],
'Factorials A230960', [1, map { factorial $_ } 1..$upto]
) {
my @bt = boustrophedon_transform @$seq;
say "\n$name:\n" . join ' ', @bt[0..14];
say "100th term: " . abbr $bt[$upto-1];
}
</syntaxhighlight>
{{out}}
<pre>1 followed by 0's A000111:
1 1 1 2 5 16 61 272 1385 7936 50521 353792 2702765 22368256 199360981
100th term: 45608516616801111821..68991870306963423232 (137 digits)
 
All-1's A000667:
1 2 4 9 24 77 294 1309 6664 38177 243034 1701909 13001604 107601977 959021574
100th term: 21939873756450413339..30507739683220525509 (138 digits)
 
(-1)^n A062162:
1 0 0 1 0 5 10 61 280 1665 10470 73621 561660 4650425 41441530
100th term: 94810791122872999361..65519440121851711941 (136 digits)
 
Primes A000747:
2 5 13 35 103 345 1325 5911 30067 172237 1096319 7677155 58648421 485377457 4326008691
100th term: 98967625721691921699..78027927576425134967 (138 digits)
 
Fibbonaccis A000744:
1 2 5 14 42 144 563 2526 12877 73778 469616 3288428 25121097 207902202 1852961189
100th term: 42390820205259437020..42168748587048986542 (138 digits)
 
Factorials A230960:
1 2 5 17 73 381 2347 16701 134993 1222873 12279251 135425553 1627809401 21183890469 296773827547
100th term: 31807659526053444023..65546706672657314921 (157 digits)</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">-->
2,392

edits