Hofstadter Figure-Figure sequences: Difference between revisions
Content added Content deleted
m (→{{header|FreeBASIC}}: -fix) |
Not a robot (talk | contribs) (Add CLU) |
||
Line 667: | Line 667: | ||
71 72 73 74 75 76 77 78 79 80 81 82 84 85 86 87 88 89 90 91 |
71 72 73 74 75 76 77 78 79 80 81 82 84 85 86 87 88 89 90 91 |
||
92 93 94 95 96 97 99 100 101 102 103 104 105 106 107 108 109 110 111 112</lang> |
92 93 94 95 96 97 99 100 101 102 103 104 105 106 107 108 109 110 111 112</lang> |
||
=={{header|CLU}}== |
|||
<lang clu>figfig = cluster is ffr, ffs |
|||
rep = null |
|||
ai = array[int] |
|||
own R: ai := ai$[1] |
|||
own S: ai := ai$[2] |
|||
% Extend R and S until R(n) is known |
|||
extend = proc (n: int) |
|||
while n > ai$high(R) do |
|||
next: int := ai$top(R) + S[ai$high(R)] |
|||
ai$addh(R, next) |
|||
while ai$top(S) < next-1 do |
|||
ai$addh(S, ai$top(S)+1) |
|||
end |
|||
ai$addh(S, next+1) |
|||
end |
|||
end extend |
|||
ffr = proc (n: int) returns (int) |
|||
extend(n) |
|||
return(R[n]) |
|||
end ffr |
|||
ffs = proc (n: int) returns (int) |
|||
while n > ai$high(S) do |
|||
extend(ai$high(R) + 1) |
|||
end |
|||
return(S[n]) |
|||
end ffs |
|||
end figfig |
|||
start_up = proc () |
|||
ai = array[int] |
|||
po: stream := stream$primary_output() |
|||
% Print R[1..10] |
|||
stream$puts(po, "R[1..10] =") |
|||
for i: int in int$from_to(1,10) do |
|||
stream$puts(po, " " || int$unparse(figfig$ffr(i))) |
|||
end |
|||
stream$putl(po, "") |
|||
% Count the occurrences of 1..1000 in R[1..40] and S[1..960] |
|||
occur: ai := ai$fill(1, 1000, 0) |
|||
for i: int in int$from_to(1, 40) do |
|||
occur[figfig$ffr(i)] := occur[figfig$ffr(i)] + 1 |
|||
end |
|||
for i: int in int$from_to(1, 960) do |
|||
occur[figfig$ffs(i)] := occur[figfig$ffs(i)] + 1 |
|||
end |
|||
% See if they all occur exactly once |
|||
begin |
|||
for i: int in int$from_to(1, 1000) do |
|||
if occur[i] ~= 1 then exit wrong(i) end |
|||
end |
|||
stream$putl(po, |
|||
"All numbers 1..1000 occur exactly once in R[1..40] U S[1..960].") |
|||
end except when wrong(i: int): |
|||
stream$putl(po, "Error: " || |
|||
int$unparse(i) || " occurs " || int$unparse(occur[i]) || " times.") |
|||
end |
|||
end start_up</lang> |
|||
{{out}} |
|||
<pre>R[1..10] = 1 3 7 12 18 26 35 45 56 69 |
|||
All numbers 1..1000 occur exactly once in R[1..40] U S[1..960].</pre> |
|||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |