Fibonacci numbers in Roco
Example for versions
Roco 20071014
This example uses iterative definition of Fibonacci numbers by saving them all in cells [2]..[17]. Cell [0] stores the index of the next number to be calculated, and cell [1] is used as temporary storage. Loops are implemented as coroutines, since by definition coroutines loop until another coroutine is called or execution is interrupted with ac command.
co calc{
/* break the loop when the counter is 2+16, since numbers start with cell 2 */
eq [1] [0] 18
if [1] ac
/* calculate next number and store it to [[0]]*/
sub [1] [0] 1
set [[0]] [[1]]
sub [1] [0] 2
add [[0]] [[0]] [[1]]
/* output */
iout [[0]]
cout 44
cout 32
/* increment counter */
add [0] [0] 1
}
/* initialize with first Fibonacci numbers */
set [0] 4
set [2] 1
set [3] 1
iout [2]
cout 44
cout 32
iout [3]
cout 44
cout 32
ca calc
cout 46
cout 46
cout 46
ac
Comments
]]>blog comments powered by Disqus
]]>