Fibonacci sequence: Difference between revisions

Content added Content deleted
(→‎{{header|Forth}}: Add version with support for negative indexes)
(Add CLU)
Line 2,703:
(println (<!! c))))))
</lang>
 
=={{header|CLU}}==
<lang clu>% Generate Fibonacci numbers
fib = iter () yields (int)
a: int := 0
b: int := 1
while true do
yield (a)
a, b := b, a+b
end
end fib
 
% Grab the n'th value from an iterator
nth = proc [T: type] (g: itertype () yields (T), n: int) returns (T)
for v: T in g() do
if n<=0 then return (v) end
n := n-1
end
end nth
 
% Print a few values
start_up = proc ()
po: stream := stream$primary_output()
% print values coming out of the fibonacci iterator
% (which are generated one after the other without delay)
count: int := 0
for f: int in fib() do
stream$putl(po, "F(" || int$unparse(count) || ") = " || int$unparse(f))
count := count + 1
if count = 15 then break end
end
% print a few random fibonacci numbers
% (to do this it has to restart at the beginning for each
% number, making it O(N))
fibs: sequence[int] := sequence[int]$[20,30,50]
for n: int in sequence[int]$elements(fibs) do
stream$putl(po, "F(" || int$unparse(n) || ") = "
|| int$unparse(nth[int](fib, n)))
end
end start_up</lang>
{{out}}
<pre>F(0) = 0
F(1) = 1
F(2) = 1
F(3) = 2
F(4) = 3
F(5) = 5
F(6) = 8
F(7) = 13
F(8) = 21
F(9) = 34
F(10) = 55
F(11) = 89
F(12) = 144
F(13) = 233
F(14) = 377
F(20) = 6765
F(30) = 832040
F(50) = 12586269025</pre>
 
=={{header|CMake}}==