Boustrophedon transform: Difference between revisions

Added Algol 68
(Added Perl)
(Added Algol 68)
Line 45:
 
 
 
=={{header|ALGOL 68}}==
Basic task - assumes LONG INT is large enough, e.g. 64 bits, 39 bits would do...
<syntaxhighlight lang="algol68">
BEGIN # apply a Boustrophedon Transform to a few sequences #
# returns the sequence generated by transforming s #
PROC boustrophedon transform = ( []LONG INT s )[]LONG INT:
BEGIN
[]LONG INT a = s[ AT 0 ]; # a is s with lower bound revised to 0 #
[ 0 : UPB a, 0 : UPB a ]LONG INT t;
t[ 0, 0 ] := a[ 0 ];
FOR k TO UPB a DO
t[ k, 0 ] := a[ k ];
FOR n TO k DO
t[ k, n ] := t[ k, n - 1 ] + t[ k - 1, k - n ]
OD
OD;
[ 0 : UPB a ]LONG INT b;
FOR n FROM 0 TO UPB b DO b[ n ] := t[ n, n ] OD;
b
END # boustrophedon transform # ;
# prints the transformed sequence generated from a #
PROC print transform = ( STRING name, []LONG INT a )VOID:
BEGIN
[]LONG INT b = boustrophedon transform( a );
print( ( name, " generates:", newline ) );
FOR i FROM LWB b TO UPB b DO print( ( " ", whole( b[ i ], 0 ) ) ) OD;
print( ( newline ) )
END # print transform # ;
BEGIN # test cases #
print transform( "1, 0, 0, 0, ..."
, []LONG INT( 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )
);
print transform( "1, 1, 1, 1, ..."
, []LONG INT( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 )
);
print transform( "1, -1, 1, -1, ..."
, []LONG INT( 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1 )
);
print transform( "primes"
, []LONG INT( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 )
);
print transform( "fibnonacci numbers"
, []LONG INT( 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 )
);
[ 0 : 14 ]LONG INT factorial;
factorial[ 0 ] := 1;
FOR i TO UPB factorial DO factorial[ i ] := i * factorial[ i - 1 ] OD;
print transform( "factorial numbers", factorial )
END
END
</syntaxhighlight>
{{out}}
<pre>
1, 0, 0, 0, ... generates:
1 1 1 2 5 16 61 272 1385 7936 50521 353792 2702765 22368256 199360981
1, 1, 1, 1, ... generates:
1 2 4 9 24 77 294 1309 6664 38177 243034 1701909 13001604 107601977 959021574
1, -1, 1, -1, ... generates:
1 0 0 1 0 5 10 61 280 1665 10470 73621 561660 4650425 41441530
primes generates:
2 5 13 35 103 345 1325 5911 30067 172237 1096319 7677155 58648421 485377457 4326008691
fibnonacci numbers generates:
1 2 5 14 42 144 563 2526 12877 73778 469616 3288428 25121097 207902202 1852961189
factorial numbers generates:
1 2 5 17 73 381 2347 16701 134993 1222873 12279251 135425553 1627809401 21183890469 296773827547
</pre>
 
=={{header|J}}==
3,038

edits