Riordan numbers: Difference between revisions
Content added Content deleted
(Added C++ solution) |
SqrtNegInf (talk | contribs) (Added Perl) |
||
Line 560: | Line 560: | ||
54022715451 154000562758 439742222071 1257643249140 |
54022715451 154000562758 439742222071 1257643249140 |
||
</pre> |
</pre> |
||
=={{header|Perl}}== |
|||
<syntaxhighlight lang="perl" line>use v5.36; |
|||
use bigint; |
|||
use experimental <builtin for_list>; |
|||
use List::Util 'max'; |
|||
use List::Lazy 'lazy_list'; |
|||
use Lingua::EN::Numbers qw(num2en_ordinal); |
|||
sub abbr ($d) { my $l = length $d; $l < 41 ? $d : substr($d,0,20) . '..' . substr($d,-20) . " ($l digits)" } |
|||
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r } |
|||
sub table ($c, @V) { my $t = $c * (my $w = 2 + max map { length } @V); ( sprintf( ('%'.$w.'s')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr } |
|||
my @riordan; |
|||
my $riordan_lazy = lazy_list { state @r = (1,0); state $n = 1; $n++; push @r, ($n-1) * (2*$r[1] + 3*$r[0]) / ($n+1) ; shift @r }; |
|||
push @riordan, $riordan_lazy->next() for 1..1e4; |
|||
say 'First thirty-two Riordan numbers:'; |
|||
say table 4, map { comma $_ } @riordan[0..31]; |
|||
say 'The ' . num2en_ordinal($_) . ': ' . abbr $riordan[$_ - 1] for 1e3, 1e4;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First thirty-two Riordan numbers: |
|||
1 0 1 1 |
|||
3 6 15 36 |
|||
91 232 603 1,585 |
|||
4,213 11,298 30,537 83,097 |
|||
227,475 625,992 1,730,787 4,805,595 |
|||
13,393,689 37,458,330 105,089,229 295,673,994 |
|||
834,086,421 2,358,641,376 6,684,761,125 18,985,057,351 |
|||
54,022,715,451 154,000,562,758 439,742,222,071 1,257,643,249,140 |
|||
The one thousandth: 51077756867821111314..79942013897484633052 (472 digits) |
|||
The ten thousandth: 19927418577260688844..71395322020211157137 (4765 digits)</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |