Riordan numbers: Difference between revisions

Add PARI/GP implementation
(Riordan numbers in various BASIC dialents (QBasic, True BASIC and Yabasic))
(Add PARI/GP implementation)
 
(25 intermediate revisions by 20 users not shown)
Line 1:
{{draft task}}
 
Riordan numbers show up in several places in set theory. They are closely related to [[Motzkin numbers]], and may be used to derive them.
Line 24:
 
<br>
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F riordan(nn)
V a = [BigInt(1), 0, 1]
L(n) 3 .< nn
a.append((n - 1) * (2 * a[n - 1] + 3 * a[n - 2]) I/ (n + 1))
R a
 
V rios = riordan(10'000)
 
L(i) 32
print(f:‘{commatize(rios[i]):18}’, end' I (i + 1) % 4 == 0 {"\n"} E ‘’)
 
print(‘The 1,000th Riordan has ’String(rios[999]).len‘ digits.’)
print(‘The 10,000th Rirdan has ’String(rios[9999]).len‘ digits.’)
</syntaxhighlight>
 
{{out}}
<pre>
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 1,000th Riordan has 472 digits.
The 10,000th Rirdan has 4765 digits.
</pre>
 
=={{header|Action!}}==
{{Trans|ALGOL W}}
Line 205 ⟶ 238:
13393689 37458330
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on riordanNumbers(n)
set a to {1, 0}
repeat with n from 3 to n
set {an2, an1} to a's items -2 thru -1
set a's end to (n - 2) * (an1 + an1 + 3 * an2) div n
end repeat
return a
end riordanNumbers
 
on intToText(int, separator)
set groups to {}
repeat while (int > 999)
set groups's beginning to ((1000 + (int mod 1000 as integer)) as text)'s text 2 thru 4
set int to int div 1000
end repeat
set groups's beginning to int as integer
return join(groups, separator)
end intToText
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set riordans to riordanNumbers(32)
set columnWidth to (count intToText(riordans's item 32, ",")) + 2
set output to {"1st 32 Riordan numbers:"}
set row to {}
repeat with i from 1 to 32 by 4
repeat with j from i to (i + 3)
set end of row to text -columnWidth thru -1 of ¬
(" " & intToText(riordans's item j, ","))
end repeat
set end of output to join(row, "")
set row to {}
end repeat
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"1st 32 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"</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">riordan: function [n].memoize[
if zero? n -> return 1
if one? n -> return 0
 
return ((n-1) * ((2*riordan n-1) + 3*riordan n-2)) / n+1
]
 
riordans: map 0..31 => riordan
 
loop split.every: 4 riordans 'x ->
print map x 's -> pad to :string s 13</syntaxhighlight>
 
{{out}}
 
<pre> 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</pre>
 
=={{header|BASIC}}==
Line 262 ⟶ 378:
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|uBasic/4tH}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">l = 31
c = 0
 
Dim @r(l)
 
Print "First 32 Riordan numbers:"
Proc _Riordan(l)
 
For i = 0 To l
Print Using " ____________#"; @r(i);
c = c + 1
If c % 4 = 0 Then Print
Next
 
End
 
_Riordan
Param (1)
Local (1)
If (a@ < 0) = 0 Then
@r(0) = 1
If (a@ < 1) = 0 Then
@r(1) = 0
For b@ = 2 To a@
@r(b@) = ((b@-1) * ((2 * @r(b@-1)) + (3 * @r(b@-2)))) / (b@+1)
Next
EndIf
EndIf
Return</syntaxhighlight>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
Line 291 ⟶ 439:
fi
end sub</syntaxhighlight>
 
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat">( ( a
=
. !arg:0&1
| !arg:1&0
| !(!arg$A):>0
| (!arg+-1)
* (2*a$(!arg+-1)+3*a$(!arg+-2))
* (!arg+1)^-1
: ?(!arg$A)
)
& "Create a table A that is big enough to memoize 10000 values. All values are initialized to 0."
& "Table elements are selected by using the syntax !index$tableName"
& tbl$(A,10000)
& -1:?n
& whl
' (!n+1:~>32:?n&out$(a$!n))
& "Theoretically, one could just ask for a(10000), but without first computing a(n) for all n < 10000 there is a risk a risk of stack overflow."
& whl'(!n+1:~>10000:?n&a$!n)
& "Apply string pattern matching @(subject:pattern) the the value of a(n) with the special pattern [?variable to find the total length of the string."
& @(a$999:? [?l1000)
& @(a$9999:? [?l10000)
& out$(str$("a(999) has " !l1000 " digits."))
& out$(str$("a(9999) has " !l10000 " digits."))
)</syntaxhighlight>
 
{{out}}
<pre>
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
3602118427251
a(999) has 472 digits.
a(9999) has 4765 digits.
</pre>
 
=={{header|C++}}==
Line 323 ⟶ 536:
}
 
std::string to_string(const big_int& num, size_t nmax_digits) {
std::string str = num.get_str();
size_t len = str.size();
if (len > nmax_digits)
str = str.substrreplace(0, nmax_digits / 2), +len - max_digits, "..." + str.substr(len - n / 2);
return str;
}
Line 367 ⟶ 580:
The 10000th is 19927418577260688844...71395322020211157137 (4765 digits).
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
cnt = 32
print "First " & cnt & " Riordan numbers:"
app = 1 ; ap = 0
print app ; print ap
for n = 2 to cnt - 1
a = (n - 1) * (2 * ap + 3 * app) / (n + 1)
print a
app = ap
ap = a
.
</syntaxhighlight>
 
 
=={{header|F_Sharp|F#}}==
Line 416 ⟶ 644:
834086421 2358641376 6684761125 18985057351
54022715451 154000562758 439742222071 1257643249140</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_limit = 31
 
local fn Riordan( n as long, a(_limit) as long )
long i
if ( n >= 0 )
a(0) = 1
if ( n >= 1 )
a(1) = 0
for i = 2 to n
a(i) = ( ( i - 1 ) * ( ( 2 * a(i-1) ) + ( 3 * a(i-2) ) ) ) / ( i + 1 )
next
end if
end if
end fn
 
long i, count = 0, r(_limit)
printf @"First 32 Riordan numbers:"
fn Riordan( _limit, r(0) )
 
for i = 0 to 23
printf @"%16ld\b", r(i)
count++
if count mod 4 == 0 then print
next
 
count = 0
for i = 24 to _limit
printf @"%16s\b", fn StringUTF8String( Str(r(i)) )
count++
if count mod 4 == 0 then print
next
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
First 32 Riordan numbers:
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
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">--------------------- RIORDAN NUMBERS --------------------
 
riordans :: [Integer]
riordans =
1 :
0 :
zipWith
div
( zipWith
(*)
[1 ..]
( zipWith
(+)
((2 *) <$> tail riordans)
((3 *) <$> riordans)
)
)
[3 ..]
 
-------------------------- TESTS -------------------------
main :: IO ()
main =
putStrLn "First 32 Riordan terms:"
>> mapM_ print (take 32 riordans)
>> mapM_
( \x ->
putStrLn $
concat
[ "\nDigit count of ",
show x,
"th Riordan term:\n",
(show . length . show)
(riordans !! pred x)
]
)
[1000, 10000]</syntaxhighlight>
{{Out}}
<pre>First 32 Riordan terms:
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
 
Digit count of 1000th Riordan term:
472
 
Digit count of 10000th Riordan term:
4765</pre>
 
=={{header|J}}==
Line 424 ⟶ 780:
#":(1e4-1){riordanext^:(1e4) x:1 0
4765</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.List;
 
public final class RiordanNumbers {
 
public static void main(String[] args) {
final int limit = 10_000;
final BigInteger THREE = BigInteger.valueOf(3);
BigInteger[] riordans = new BigInteger[limit];
riordans[0] = BigInteger.ONE;
riordans[1] = BigInteger.ZERO;
for ( int n = 2; n < limit; n++ ) {
BigInteger term = BigInteger.TWO.multiply(riordans[n - 1]).add(THREE.multiply(riordans[n - 2]));
riordans[n] = BigInteger.valueOf(n - 1).multiply(term).divide(BigInteger.valueOf(n + 1));
}
System.out.println("The first 32 Riordan numbers:");
for ( int i = 0; i < 32; i++ ) {
System.out.print(String.format("%14d%s", riordans[i], ( i % 4 == 3 ? "\n" :" " )));
}
System.out.println();
for ( int count : List.of( 1_000, 10_000 ) ) {
int length = riordans[count - 1].toString().length();
System.out.println("The " + count + "th Riordan number has " + length + " digits");
}
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
The first 32 Riordan numbers:
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
 
The 1000th Riordan number has 472 digits
The 10000th Riordan number has 4765 digits
</pre>
 
=={{header|jq}}==
 
The C implementation of jq has sufficient arithmetic accuracy for the
first task, but because of the stretch task, the Go implementation has been
used as gojq's integer arithmetic has unbounded accuracy.
 
Using the program below to calculate the first 100,000 Riordan numbers, gojq takes about
4.7 seconds on a 3GHz machine.
 
<syntaxhighlight lang=jq>
def riordan:
{ai: 1, a1: 0}
| ., foreach range(1; infinite) as $i (.;
{ai: ( ($i-1) * (2*.ai + 3*.a1) / ($i+1)),
a1: .ai } )
| .ai ;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def snip($n):
tostring|lpad(6)
+ ($n | tostring | "th: \(.[:10]) .. \(.[-10:]) (\(length) digits)" );
 
"First 32 Riordan numbers:",
foreach limit(100000; riordan) as $riordan (0; .+1;
if . <= 32 then $riordan
elif . == 1000 or . == 10000 or . == 100000 then snip($riordan)
else empty end)
</syntaxhighlight>
 
'''Invocation''': jq -nr -f riordan.jq
 
{{output}}
<pre>
First 32 Riordan numbers:
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
1000th: 5107775686 .. 7484633052 (472 digits)
10000th: 1992741857 .. 0211157137 (4765 digits)
100000th: 5156659846 .. 4709713332 (47704 digits)
</pre>
 
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">""" julia example for rosettacode.org/wiki/Riordan_number """
 
using Formatting
 
const riordans = zeros(BigInt, 10000)
riordans[begin] = 1
 
for i in firstindex(riordans)+1:lastindex(riordans)-1
riordans[i + 1] = (i - 1) * (2 * riordans[i] + 3 * riordans[i - 1]) ÷ (i + 1)
end
 
for i in 0:31
print(rpad(format(riordans[begin+i], commas = true), 18), (i + 1) % 4 == 0 ? "\n" : "")
end
println("\nThe 1,000th Riordan has $(length(string(riordans[1000]))) digits.")
println("The 10,000th Riordan has $(length(string(riordans[10_000]))) digits.")
</syntaxhighlight>{{out}}
<pre>
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 1,000th Riordan has 472 digits.
The 10,000th Riordan has 4765 digits.
</pre>
 
=={{header|Lua}}==
{{Trans|ALGOL 68|basic task only, as Lua integers are (usually) limited to 2^53}}
<syntaxhighlight lang="lua">
do -- Riordan numbers
 
local function riordan( n ) -- returns a table of the Riordan numbers 0 .. n
local a = {}
if n >= 0 then
a[ 0 ] = 1
if n >= 1 then
a[ 1 ] = 0
for i = 2, n do
a[ i ] = math.floor( ( ( i - 1 )
* ( ( 2 * a[ i - 1 ] )
+ ( 3 * a[ i - 2 ] )
)
)
/ ( i + 1 )
)
end
end
end
return a
end
local function commatise( unformatted ) -- returns a string representation of n with commas
local result, chCount = "", 0
for c = #unformatted, 1, -1 do
if chCount <= 2 then
chCount = chCount + 1
else
chCount = 1
result = ( unformatted:sub( c, c ) == " " and " " or "," )..result
end
result = unformatted:sub( c, c )..result
end
return result
end
 
do -- show the first 32 Riordann numbers
local r, shown = riordan( 31 ), 0
for i = 0, #r do
shown = ( shown + 1 ) % 4
io.write( commatise( string.format( "%15d", r[ i ] ) )
, ( shown == 0 and "\n" or "" )
)
end
end
end
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">
Riordan[N_] :=
Module[{a = {1, 0, 1}},
Do[AppendTo[a, ((n - 1) (2 a[[n]] + 3 a[[n - 1]])/(n + 1))], {n, 3,
N}];
a]
 
rios = Riordan[10000];
 
Do[Print[ToString@NumberForm[rios[[i]], DigitBlock -> 3]], {i, 32}]
 
Print["The 1,000th Riordan number has ", IntegerLength[rios[[1000]]],
" digits."];
Print["The 10,000th Riordan number has ",
IntegerLength[rios[[10000]]], " digits."];
</syntaxhighlight>
{{out}}
<pre>
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 1,000th Riordan number has 472 digits.
The 10,000th Riordan number has 4765 digits.
 
</pre>
 
=={{header|Nim}}==
===Task===
<syntaxhighlight lang="Nim">import std/strformat
 
iterator riordan(): int =
var prev = 1
var curr = 0
yield prev
var n = 1
while true:
yield curr
inc n
let next = (n - 1) * (2 * curr + 3 * prev) div (n + 1)
prev = curr
curr = next
 
echo &"First 32 Riordan numbers:"
var count = 0
for n in riordan():
inc count
stdout.write &"{n:<13}"
stdout.write if count mod 4 == 0: '\n' else: ' '
if count == 32: break
</syntaxhighlight>
 
{{out}}
<pre>First 32 Riordan numbers:
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
</pre>
 
===Stretch task===
{{libheader|Nim-Integers}}
<syntaxhighlight lang="Nim">import std/strformat
import integers
 
iterator riordan(): Integer =
var prev = newInteger(1)
var curr = newInteger(0)
yield prev
var n = 1
while true:
yield curr
inc n
let next = (n - 1) * (2 * curr + 3 * prev) div (n + 1)
prev = curr
curr = next
 
var count = 0
for n in riordan():
inc count
if count in [1000, 10000]:
echo &"The {count}th Riordan number has {len($n)} digits."
if count == 10000: break
</syntaxhighlight>
 
{{out}}
<pre>The 1000th Riordan number has 472 digits.
The 10000th Riordan number has 4765 digits.
</pre>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="PARI/GP">
\\ Increase the stack size if necessary
default(parisize, "32M"); \\ Increase to 32MB, adjust if necessary
 
Riordan(N) = {
my(a = vector(N));
a[1] = 1; a[2] = 0; a[3] = 1;
for (n = 3, N-1,
a[n+1] = ((n - 1) * (2 * a[n] + 3 * a[n-1]) \ (n + 1)); \\ Integer division
);
return(a);
}
 
rios = Riordan(10000);
 
\\ Now print the first 32 elements in the desired format
for (i = 1, 32,{
print1(rios[i]," ")
});
print("")
 
\\ Print the number of digits for the 1000th and 10000th Riordan numbers
print("The 1,000th Riordan has ", #digits(rios[1000]), " digits.");
print("The 10,000th Riordan has ", #digits(rios[10000]), " digits.");
</syntaxhighlight>
{{out}}
<pre>
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
The 1,000th Riordan has 472 digits.
The 10,000th Riordan has 4765 digits.
 
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl" line>use v5.36;
use bigint try => 'GMP';
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}}==
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">an</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_addmul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">an</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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;">"First 32 Riordan numbers:\n%s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">32</span><span style="color: #0000FF;">],</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%17s"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1e3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1e4</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
First 32 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 (4,765 digits)
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">showRiordan: procedure options( main ); /* find some Riordan numbers */
Line 770 ⟶ 1,569:
834086421 2358641376 6684761125 18985057351
54022715451 154000562758 439742222071 1257643249140
</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}}==
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10000</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">limit</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">an</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_addmul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">assert</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">an</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">an</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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;">"First 32 Riordan numbers:\n%s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">32</span><span style="color: #0000FF;">],</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%17s"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #008080;">in</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1e3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1e4</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">do</span>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
First 32 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 (4,765 digits)
</pre>
 
Line 869 ⟶ 1,598:
The 10,000th Rirdan has 4765 digits.
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ dup -1 peek 2 *
over -2 peek 3 * +
over size tuck 1 - *
swap 1+ /
join ] is nextterm ( [ --> [ )
 
 
say "first 32 Riordan numbers: "
' [ 1 0 ]
30 times nextterm
witheach [ echo sp ]
cr cr
say "1000th Riordan number has "
' [ 1 0 ]
1000 2 - times nextterm
-1 peek number$ size echo
say " digits"
cr cr
say "10000th Riordan number has "
' [ 1 0 ]
10000 2 - times nextterm
-1 peek number$ size echo
say " digits"
</syntaxhighlight>
 
{{out}}
 
<pre>first 32 Riordan numbers: 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
 
1000th Riordan number has 472 digits
 
10000th Riordan number has 4765 digits</pre>
 
=={{header|Raku}}==
Line 894 ⟶ 1,658:
The one thousandth: 51077756867821111314..79942013897484633052 (472 digits)
The ten thousandth: 19927418577260688844..71395322020211157137 (4765 digits)</pre>
 
=={{header|RPL}}==
{{works with|HP|28}}
Needed to use unsigned integers to avoid the loss of the last digit of a(31).
≪ { #1 #0 }
'''WHILE''' DUP2 SIZE 1 - > '''REPEAT'''
DUP SIZE 1 -
DUP2 GETI 3 * 4 ROLLD
GET 2 * ROT +
OVER * SWAP 2 + /
+
'''END''' SWAP DROP
≫ '<span style="color:blue">RIORDAN</span>' STO
 
31 <span style="color:blue">RIORDAN</span>
{{out}}
<pre>
1: { #1d #0d #1d #1d #3d #6d #15d #36d #91d #232d #603d #1585d #4213d 11298d #30537d #83097d #227475d #625992d #1730787d #4805595d #13393689d #37458330d #105089229d #295673994d #834086421d #2358641376d #6684761125d #18985057351d #54022715451d #154000562758d #439742222071d #1257643249140d }
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
import java.math.BigInteger
import scala.collection.mutable.ArrayBuffer
 
object RiordanNumbers extends App {a
val limit = 10000
val THREE = BigInteger.valueOf(3)
 
val riordans: ArrayBuffer[BigInteger] = ArrayBuffer.fill(limit)(BigInteger.ZERO)
riordans(0) = BigInteger.ONE
riordans(1) = BigInteger.ZERO
 
for (n <- 2 until limit) {
val term = BigInteger.TWO.multiply(riordans(n - 1)).add(THREE.multiply(riordans(n - 2)))
riordans(n) = BigInteger.valueOf(n - 1).multiply(term).divide(BigInteger.valueOf(n + 1))
}
 
println("The first 32 Riordan numbers:")
for (i <- 0 until 32) {
print(f"${riordans(i)}%14d")
if (i % 4 == 3) println()
else print(" ")
}
println()
 
List(1000, 10000).foreach { count =>
val length = riordans(count - 1).toString.length
println(s"The ${count}th Riordan number has $length digits")
}
}
</syntaxhighlight>
{{out}}
<pre>
The first 32 Riordan numbers:
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
 
The 1000th Riordan number has 472 digits
The 10000th Riordan number has 4765 digits
 
</pre>
 
 
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program riordan;
a := {[0, 1], [1, 0]};
 
loop for n in [2..9999] do
a(n) := (n-1)*(2*a(n-1) + 3*a(n-2)) div (n+1);
end loop;
 
loop for n in [0..31] do
putchar(lpad(str a(n), 15));
if n mod 4=3 then print; end if;
end loop;
 
loop for n in [999, 9999] do
print("The", str (n+1)+"th Riordan number has", #str a(n), "digits.");
end loop;
end program;</syntaxhighlight>
{{out}}
<pre> 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
The 1000th Riordan number has 472 digits.
The 10000th Riordan number has 4765 digits.</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func riordan(n) is cached {
return 1 if (n == 0)
return 0 if (n == 1)
(n-1) * (2*__FUNC__(n-1) + 3*__FUNC__(n-2)) / (n+1)
}
 
say 32.of(riordan)
 
for n in (1e3, 1e4) {
var s = Str(riordan(n-1))
say "#{'%6s' % n.commify}th term: #{s.first(20)}..#{s.last(20)} (#{s.len} digits)"
}</syntaxhighlight>
{{out}}
<pre>
[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]
1,000th term: 51077756867821111314..79942013897484633052 (472 digits)
10,000th term: 19927418577260688844..71395322020211157137 (4765 digits)
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-gmp}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./gmp" for Mpz
import "./fmt" for Fmt
 
337

edits