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=20 21; de= d + 6; numeric digits de /*use more precision (the default is 9)*/
parse arg N . /*get optional argument from the C.L. */
if N=='' | N=="," then N=42 42 /*Not specified? Then use the default.*/
say center('N', 53) center("length", 12de) center('entropy', de) center("Fib word", 56)
say copies('─', 53) copies("─" , 12de) copies('─' , de) copies("─" , 56)
c= 1 /*initialize [↓]the 1st displayvalue for N fibonacci wordsentropy. */
do j=1 for N; if j==2 then c=0 /*test for[↓] the casedisplay of JN equals fibonacci 2words. */
if j==32 then parse value 1c= 0 with a b /* " " " " " /*test "for the case of " J equals 32. */
if j>2==3 then c=bparse ||value a; 1 L=length(c) 0 with a b /*calculate the FIBword" if we" need to " " " " " 3. */
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, length is: ' L"}"'
say right(j,4 2) right( commas(L),12 de) ' ' entropy() " " Fw
a= b; b=c c /*define the new values for A and 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.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
!.0=length( space( translate(c,,1), 0)) /*efficient way to count the "zeroes".*/
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. */
$!.0=$ -!._/L*log2length(space(!._/Ltranslate(c,, 1), 0)) /*addefficient way (negatively)to count the entropies "zeroes". */
!.1= L - !.0; end $= 0 /*idefine 1st fib─word; initial entropy.*/
!.1=L-!.0; $=0; do i=1 for 2; _= i - 1 /*construct character from the ether. */
$= $ - !._ / 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) /*normalize the sum (S) number. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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>
'''{{out|output''' |text=&nbsp; forwhen using the first 42 Fibonaccidefault wordsinput:}}
<pre>
N N length entropy Fib word
──────── ─────────────────────────────────────── ───────────────────────────────────────────────────── ────────────────────────────────────────────────────────
1 1 1 0 1
2 2 1 0 0
3 3 2 1 01
4 4 3 0.91829583405448951479918295834054489514787 010
5 5 5 0.97095059445466863900970950594454668638998 01001
6 6 8 0.95443400292496496454954434002924964964536 01001010
7 7 13 0.96123660472287587275961236604722875872754 0100101001001
8 8 21 0.95871188297713180865958711882977131808650 010010100100101001010
9 9 34 0.95968689377421693320959686893774216933196 0100101001001010010100100101001001
10 10 55 0.95931603205437767775959316032054377677752 0100101001001010010100100101001001010010100100101001010
11 11 89 0.95945791583866946166959457915838669461656 {the word is too wide to display, length is: 89}
12 12 144 0.95940375422102292947959403754221022929465 {the word is too wide to display, length is: 144}
13 13 233 0.95942444695598675869959424446955986758690 {the word is too wide to display, length is: 233}
14 14 377 0.95941654374044073872959416543740440738719 {the word is too wide to display, length is: 377}
15 15 610 0.95941956260314415023959419562603144150234 {the word is too wide to display, length is: 610}
16 16 987 0.95941840951522431271959418409515224312708 {the word is too wide to display, length is: 987}
17 17 1597 1,597 0.95941884995780985568959418849957809855676 {the word is too wide to display, length is: 1597}
18 18 2584 2,584 0.95941868172403210665959418681724032106650 {the word is too wide to display, length is: 2584}
19 19 4181 4,181 0.95941874598366381433959418745983663814327 {the word is too wide to display, length is: 4181}
20 20 6765 6,765 0.95941872143867541464959418721438675414636 {the word is too wide to display, length is: 6765}
21 21 10946 10,946 0.95941873081402772313959418730814027723133 {the word is too wide to display, length is: 10946}
22 22 17711 17,711 0.95941872723296194271959418727232961942711 {the word is too wide to display, length is: 17711}
23 23 28657 28,657 0.95941872860080737603959418728600807376027 {the word is too wide to display, length is: 28657}
24 24 46368 46,368 0.95941872807833691494959418728078336914941 {the word is too wide to display, length is: 46368}
25 25 75025 75,025 0.95941872827790287341959418728277902873408 {the word is too wide to display, length is: 75025}
26 26 121393 121,393 0.95941872820167546034959418728201675460337 {the word is too wide to display, length is: 121393}
27 27 196418 196,418 0.95941872823079174127959418728230791741265 {the word is too wide to display, length is: 196418}
28 28 317811 317,811 0.95941872821967031158959418728219670311578 {the word is too wide to display, length is: 317811}
29 29 514229 514,229 0.95941872822391831972959418728223918319715 {the word is too wide to display, length is: 514229}
30 30 832040 832,040 0.95941872822229572499959418728222295724991 {the word is too wide to display, length is: 832040}
31 31 1346269 1,346,269 0.95941872822291550103959418728222915501026 {the word is too wide to display, length is: 1346269}
32 32 2178309 2,178,309 0.95941872822267876765959418728222678767646 {the word is too wide to display, length is: 2178309}
33 33 3524578 3,524,578 0.95941872822276919175959418728222769191751 {the word is too wide to display, length is: 3524578}
34 34 5702887 5,702,887 0.95941872822273465282959418728222734652816 {the word is too wide to display, length is: 5702887}
35 35 9227465 9,227,465 0.95941872822274784552959418728222747845515 {the word is too wide to display, length is: 9227465}
36 36 14930352 14,930,352 0.95941872822274280635959418728222742806353 {the word is too wide to display, length is: 14930352}
37 37 24157817 24,157,817 0.95941872822274473114959418728222744731141 {the word is too wide to display, length is: 24157817}
38 38 39088169 39,088,169 0.95941872822274399594959418728222743995937 {the word is too wide to display, length is: 39088169}
39 39 63245986 63,245,986 0.95941872822274427676959418728222744276760 {the word is too wide to display, length is: 63245986}
40 40 102334155 102,334,155 0.95941872822274416950959418728222744169496 {the word is too wide to display, length is: 102334155}
41 41 165580141 165,580,141 0.95941872822274421047959418728222744210467 {the word is too wide to display, length is: 165580141}
42 42 267914296 267,914,296 0.95941872822274419482959418728222744194817 {the word is too wide to display, length is: 267914296}
</pre>