Anonymous user
Fibonacci word: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, split some compound statements, expanded the maximum possible length, added commas to the length numbers, used a template for the output section.
m (→{{header|REXX}}: added/changed comments and whitespace, split some compound statements, expanded the maximum possible length, added commas to the length numbers, used a template for the output section.) |
|||
Line 3,403:
Programming note: 32-bit Regina REXX (under Windows/XP) can execute this program with N='''42''' without exhausting system resources, the 64-bit version of Regina can calculate bigger Fibonacci words.
<lang rexx>/*REXX program displays the number of chars in a fibonacci word, and the word's entropy.*/
d=
parse arg N . /*get optional argument from the C.L. */
if N=='' | N=="," then N=
say center('N',
say copies('─',
c= 1
do j=1 for N
if j==
if j
if j>2 then c= b || a /*calculate the FIBword if we need to.*/
L= length(c) /*find the length of the fib─word C. */
if L<56 then Fw= c
else Fw= '{the word is too wide to display
say right(j,
a= b; b=
end /*j*/ /*display text msg; */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
entropy: if L==1 then return left(0, d+2) /*handle special case of one character.*/▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
!.1=L-!.0; $=0; do i=1 for 2; _=i-1 /*construct character from the ether. */▼
!.1= L - !.0;
$= $ - !._ / L * log2(!._ / L) /*add (negatively) the entropies. */
end /*i*/
if $=1 then return left(1, d+2) /*return a left─justified "1" (one). */
return format($, , d)
/*──────────────────────────────────────────────────────────────────────────────────────*/
log2: procedure; parse arg x 1 xx; ig=x>1.5; is=1-2*(ig\==1); numeric digits 5+digits()
Line 3,434 ⟶ 3,439:
do k=1; _=-_*x; z=z+_/k; if z=p then leave; p=z; end /*k*/
r=z+m; if arg()==2 then return r; return r / log2(2,.)</lang>
<pre>
N
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre>
|