Padovan sequence: Difference between revisions

Content added Content deleted
(→‎{{header|AppleScript}}: Added an AppleScript version.)
(→‎{{header|ALGOL 68}}: Compute and compare the correct number of terms of the sequences)
Line 81: Line 81:
# returns the first 0..n elements of the Padovan sequence by #
# returns the first 0..n elements of the Padovan sequence by #
# computing by truncation P(n)=floor(p^(n-1) / s + .5) #
# computing by truncation P(n)=floor(p^(n-1) / s + .5) #
# where s = 1.0453567932525329623 and p = x^3-x-1 #
# where s = 1.0453567932525329623 #
# and p = the "plastic ratio" #
OP PADOVANC = ( INT n )[]INT:
OP PADOVANC = ( INT n )[]INT:
BEGIN
BEGIN
Line 90: Line 91:
FOR i FROM LWB result TO UPB result DO
FOR i FROM LWB result TO UPB result DO
result[ i ] := SHORTEN ENTIER ( pf / s + 0.5 );
result[ i ] := SHORTEN ENTIER ( pf / s + 0.5 );
pf *:= p;
pf *:= p
OD;
OD;
result
result
END; # PADOVANC #
END; # PADOVANC #
# returns the first 0..n L System stringss of the Padovan sequence #
# returns the first 0..n L System strings of the Padovan sequence #
OP PADOVANL = ( INT n )[]STRING:
OP PADOVANL = ( INT n )[]STRING:
BEGIN
BEGIN
Line 102: Line 103:
OD;
OD;
l
l
END; # PADOVANC #
END; # PADOVANC #
# returns TRUE if a and b have the same values, FALSE otherwise #
OP = = ( []INT a, b )BOOL:
IF LWB a /= LWB b OR UPB a /= UPB b
THEN # rows are not the same size # FALSE
ELSE
BOOL result := TRUE;
FOR i FROM LWB a TO UPB a WHILE result := a[ i ] = b[ i ] DO SKIP OD;
result
FI; # = #
# returns the number of elements in a #
OP LENGTH = ( []INT a )INT: ( UPB a - LWB a ) + 1;
# returns the number of characters in s #
OP LENGTH = ( STRING s )INT: ( UPB s - LWB s ) + 1;
# returns a string representation of n #
OP TOSTRING = ( INT n )STRING: whole( n, 0 );
# generate 64 elements of the sequence and 32 L System values #
[]INT iterative = PADOVANI 64;
[]INT calculated = PADOVANC 64;
[]STRING l system = PADOVANL 32;
[ LWB l system : UPB l system ]INT l length;
FOR i FROM LWB l length TO UPB l length DO l length[ i ] := LENGTH l system[ i ] OD;
# first 20 terms #
# first 20 terms #
print( ( "First 20 terms of the Padovan Sequence", newline ) );
print( ( "First 20 terms of the Padovan Sequence", newline ) );
FOR i FROM LWB iterative TO 19 DO
[]INT iterative = PADOVANI 20;
print( ( " ", TOSTRING iterative[ i ] ) )
[]INT calculated = PADOVANC 20;
BOOL same := TRUE;
FOR i FROM LWB iterative TO UPB iterative DO
print( ( " ", whole( iterative[ i ], 0 ) ) );
IF iterative[ i ] /= calculated[ i ] THEN
same := FALSE;
print( ( " *or*", whole( calculated[ i ], 0 ) ) )
FI
OD;
OD;
print( ( newline ) );
print( ( newline ) );
print( ( "The first "
IF same THEN print( ( "Iterative and calculated values are the same", newline ) ) FI;
, TOSTRING LENGTH iterative
, " iterative and calculated values "
, IF iterative = calculated THEN "are the same" ELSE "differ" FI
, newline
)
);
# print the first 10 values of the L System strings #
# print the first 10 values of the L System strings #
[]STRING l system = PADOVANL 10;
print( ( newline, "First 10 L System strings", newline ) );
print( ( newline, "First 10 L System strings", newline ) );
FOR i FROM LWB l system TO UPB l system DO
FOR i FROM LWB l system TO 9 DO
print( ( " ", l system[ i ] ) )
print( ( " ", l system[ i ] ) )
OD;
same := TRUE;
print( ( newline, "First 10 L System string lengths", newline ) );
FOR i FROM LWB l system TO UPB l system DO
INT length = ( UPB l system[ i ] - LWB l system[ i ] ) + 1;
print( ( " ", whole( length, 0 ) ) );
IF iterative[ i ] /= length THEN
same := FALSE;
print( ( " *shoould be*", whole( iterative[ i ], 0 ) ) )
FI
OD;
OD;
print( ( newline ) );
print( ( newline ) );
print( ( "The first "
IF same THEN print( ( "Iterative values and L System lengths are the same", newline ) ) FI
, TOSTRING LENGTH l length
END</lang>
, " iterative values and L System lengths "
, IF l length = iterative[ LWB l length : UPB l length @ LWB l length ] THEN "are the same" ELSE "differ" FI
, newline
)
)
END
</lang>
{{out}}
{{out}}
<pre>
<pre>
First 20 terms of the Padovan Sequence
First 20 terms of the Padovan Sequence
1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86 114 151
1 1 1 2 2 3 4 5 7 9 12 16 21 28 37 49 65 86 114 151
Iterative and calculated values are the same
The first 64 iterative and calculated values are the same


First 10 L System strings
First 10 L System strings
A B C AB BC CAB ABBC BCCAB CABABBC ABBCBCCAB
A B C AB BC CAB ABBC BCCAB CABABBC ABBCBCCAB
First 10 L System string lengths
The first 32 iterative values and L System lengths are the same
1 1 1 2 2 3 4 5 7 9
Iterative values and L System lengths are the same
</pre>
</pre>