Riordan numbers: Difference between revisions

Add PARI/GP implementation
imported>Thebeez
(Added uBasic/4tH version)
(Add PARI/GP implementation)
 
(4 intermediate revisions by 3 users not shown)
Line 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 868 ⟶ 933:
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>
 
Line 935 ⟶ 1,117:
<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>
 
Line 1,461 ⟶ 1,677:
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}}==
337

edits