FunctionsFTW/Recursive Fibonacci
Jump to navigation
Jump to search
def("fib", (num "index") { if(eqNum(getNum("index"), 0), () { "F(0)"; returnNum(0); }, () { if(eqNum(getNum("index"), 0), () { "F(1)"; returnNum(1); }, () { "F(i-2) + F(i-1)"; returnNum(add(callNum("fib", [ "index" add(getNum("index"), neg(2)) ]), callNum("fib", [ "index" add(getNum("index"), neg(1)) ]))); }); }); }); setNum("x", 1); label("l"); print(callNum("fib", [ "index" getNum("x") ])); setNum("x", add(getNum("x"), 1)); goto("l");
Minimized (323 bytes)
def("f",(num "i"){if(eqNum(getNum("i"),0),(){returnNum(0);},(){if(eqNum(getNum("i"),0),(){returnNum(1);},(){returnNum(add(callNum("f",["i" add(getNum("i"),neg(2))]),callNum("f",["i" add(getNum("i"),neg(1))])));});});});setNum("x",1);label("l");print(callNum("f",["i" getNum("x")]));setNum("x",add(getNum("x"),1));goto("l");