Riordan numbers: Difference between revisions

m
syntax highlighting fixup automation
m (Python example)
m (syntax highlighting fixup automation)
Line 27:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32 and 3.0.3}}
Uses ALGOL 68G's LONG LONG INT which has programmer-specifiable precision. ALGOL 68G version 3 issues a warning that precision 5000 will impact performance but it still executes this program somewhat faster than version 2 does.
<langsyntaxhighlight lang="algol68">BEGIN # find some Riordan numbers #
# returns a table of the Riordan numbers 0 .. n #
OP RIORDAN = ( INT n )[]LONG INT:
Line 101:
OD
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 118:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Riordan numbers. Nigel Galloway: August 19th., 2022
let r()=seq{yield 1I; yield 0I; yield! Seq.unfold(fun(n,n1,n2)->let r=(n-1I)*(2I*n1+3I*n2)/(n+1I) in Some(r,(n+1I,r,n1)))(2I,0I,1I)}
let n=r()|>Seq.take 10000|>Array.ofSeq in n|>Array.take 32|>Seq.iter(printf "%A "); printfn "\nr[999] has %d digits\nr[9999] has %d digits" ((string n.[999]).Length) ((string n.[9999]).Length)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 131:
 
=={{header|J}}==
Sequence extender:<langsyntaxhighlight Jlang="j">riordanext=: (, (<: % >:)@# * 3 2 +/ .* _2&{.)</langsyntaxhighlight>
Task example:<langsyntaxhighlight Jlang="j"> riordanext^:(30) 1x 0
1 0 1 1 3 6 15 36 91 232 603 1585 4213 11298 30537 83097 227475 625992 1730787 4805595 13393689 37458330 105089229 295673994 834086421 2358641376 6684761125 18985057351 54022715451 154000562758 439742222071 1257643249140</langsyntaxhighlight>Stretch:<syntaxhighlight lang J="j"> #":(1e3-1){riordanext^:(1e3) x:1 0
472
#":(1e4-1){riordanext^:(1e4) x:1 0
4765</langsyntaxhighlight>
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">showRiordan: procedure options( main ); /* find some Riordan numbers */
 
%replace maxRiordan by 32;
Line 179:
end;
 
end showRiordan;</langsyntaxhighlight>
{{out}}
<pre>
Line 196:
PL/M only handles 8 and 16 bit unsigned integers but also provides two-digit BCD addition/subtraction with carry.
<br>This sample uses the BCD facility to implement 16-digit arithmetic and solve the basic task. [[Ethiopian multiplication]] and [[Egyptian division]] are used, hence the length of the sample.
<langsyntaxhighlight lang="pli">100H: /* FIND SOME RIORDAN NUMBERS */
 
DECLARE FALSE LITERALLY '0';
Line 472:
END;
 
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 486:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- mpz_get_str(comma_fill) was not working [!!!]</span>
Line 505:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %6s: %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">ordinal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span> <span style="color: #7060A8;">mpz_get_short_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 523:
 
=={{header|Python}}==
<syntaxhighlight lang="python">def Riordan(N):
a = [1, 0, 1]
for n in range(3, N):
Line 551:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
my @riordan = 1, 0, { state $n = 1; (++$n - 1) / ($n + 1) × (3 × $^a + 2 × $^b) } … *;
Line 560:
sub abr ($_) { .chars < 41 ?? $_ !! .substr(0,20) ~ '..' ~ .substr(*-20) ~ " ({.chars} digits)" }
 
say "The {.Int.&ordinal}: " ~ abr @riordan[$_ - 1] for 1e3, 1e4</langsyntaxhighlight>
{{out}}
<pre>First thirty-two Riordan numbers:
Line 578:
{{libheader|Wren-gmp}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./gmp" for Mpz
import "./fmt" for Fmt
 
Line 593:
for (i in [1e3, 1e4]) {
Fmt.print("$,8r: $20a ($,d digits)", i, a[i-1], a[i-1].toString.count)
}</langsyntaxhighlight>
 
{{out}}
10,333

edits