Riordan numbers: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 24:
 
<br>
=={{header|ALGOL 68}}==
{{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.
<lang algol68>BEGIN # find some Riordan numbers #
# returns a table of the Riordan numbers 0 .. n #
OP RIORDAN = ( INT n )[]LONG INT:
BEGIN
[ 0 : n ]LONG INT a;
IF n >= 0 THEN
a[ 0 ] := 1;
IF n >= 1 THEN
a[ 1 ] := 0;
FOR i FROM 2 TO UPB a DO
a[ i ] := ( ( i - 1 )
* ( ( 2 * a[ i - 1 ] )
+ ( 3 * a[ i - 2 ] )
)
)
OVER ( i + 1 )
OD
FI
FI;
a
END # RIORDAN # ;
# returns a string representation of n with commas #
PROC commatise = ( STRING unformatted )STRING:
BEGIN
STRING result := "";
INT ch count := 0;
FOR c FROM UPB unformatted BY -1 TO LWB unformatted DO
IF ch count <= 2 THEN
ch count +:= 1
ELSE
ch count := 1;
IF unformatted[ c ] = " " THEN " " ELSE "," FI +=: result
FI;
unformatted[ c ] +=: result
OD;
result
END; # commatise #
# returns the length of s #
OP LENGTH = ( STRING s )INT: ( UPB s - LWB s ) + 1;
BEGIN # show some Riordann numbers #
[]LONG INT r = RIORDAN 31;
INT shown := 0;
FOR i FROM LWB r TO UPB r DO
print( ( commatise( whole( r[ i ], -15 ) ) ) );
IF ( shown +:= 1 ) = 4 THEN
print( ( newline ) );
shown := 0
FI
OD
END;
BEGIN # calculate the length of the 1 000th and 10 000th Riordan numbers #
PR precision 5000 PR # allow up to 5 000 digits for LONG LONG INT #
LONG LONG INT r2 := -1, r1 := 1, r := 0;
print( ( newline ) );
FOR i FROM 2 TO 9 999 DO
r2 := r1;
r1 := r;
r := ( ( i - 1 )
* ( ( 2 * r1 )
+ ( 3 * r2 )
)
)
OVER ( i + 1 );
IF i = 999 OR i = 9 999 THEN
STRING rs = whole( r, 0 )[ @ 1 ];
print( ( "The ", whole( i + 1, -6 ), "th number is: "
, rs[ 1 : 20 ], "...", rs[ LENGTH rs - 19 : ]
, " with ", whole( LENGTH rs, -5 ), " digits"
, newline
)
)
FI
OD
END
END</lang>
{{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 1000th number is: 51077756867821111314...79942013897484633052 with 472 digits
The 10000th number is: 19927418577260688844...71395322020211157137 with 4765 digits
</pre>
 
=={{header|F_Sharp|F#}}==
<lang fsharp>
Line 36 ⟶ 129:
r[9999] has 4765 digits
</pre>
 
=={{header|J}}==
Sequence extender:<lang J>riordanext=: (, (<: % >:)@# * 3 2 +/ .* _2&{.)</lang>
3,038

edits