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 |
# 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 |
# 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 # |
|||
# 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 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
FOR i FROM LWB a TO UPB a WHILE result := a[ i ] = b[ i ] DO SKIP OD; |
|||
⚫ | |||
FI; # = # |
|||
# returns the number of elements in a # |
|||
⚫ | |||
# 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 # |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 ) ); |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
IF iterative[ i ] /= calculated[ i ] THEN |
|||
⚫ | |||
⚫ | |||
⚫ | |||
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 |
|||
⚫ | |||
, IF iterative = calculated THEN "are the same" ELSE "differ" FI |
|||
⚫ | |||
⚫ | |||
⚫ | |||
# print the first 10 values of the L System strings # |
# print the first 10 values of the L System strings # |
||
⚫ | |||
print( ( newline, "First 10 L System strings", newline ) ); |
print( ( newline, "First 10 L System strings", newline ) ); |
||
FOR i FROM LWB l system TO |
FOR i FROM LWB l system TO 9 DO |
||
print( ( " ", l system[ i ] ) ) |
print( ( " ", l system[ i ] ) ) |
||
⚫ | |||
same := TRUE; |
|||
print( ( newline, "First 10 L System string lengths", newline ) ); |
|||
⚫ | |||
⚫ | |||
print( ( " ", whole( length, 0 ) ) ); |
|||
IF iterative[ i ] /= length THEN |
|||
⚫ | |||
print( ( " *shoould be*", whole( iterative[ i ], 0 ) ) ) |
|||
⚫ | |||
OD; |
OD; |
||
print( ( newline ) ); |
print( ( newline ) ); |
||
print( ( "The first " |
|||
⚫ | |||
, TOSTRING LENGTH l length |
|||
⚫ | |||
⚫ | |||
, IF l length = iterative[ LWB l length : UPB l length @ LWB l length ] THEN "are the same" ELSE "differ" FI |
|||
, newline |
|||
) |
|||
) |
|||
END |
|||
⚫ | |||
{{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 |
||
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 |
||
The first 32 iterative values and L System lengths are the same |
|||
⚫ | |||
Iterative values and L System lengths are the same |
|||
</pre> |
</pre> |
||