//Recursive fibonacci with memoization closure

function (int)->int memoizedFib() {
    var int[] memo;
    memo := array(int, 50, 0);

    function int fib(int n) {
        if n < 2 then
            1
        else
            if memo[n] > 0 then
                memo[n]
            else
                var int answer;
                answer := fib(n-1)+fib(n-2);

                if n < memo.length then
                  memo[n] := answer
                fi;

                answer
            fi
        fi
    };
    fib
};

var (int)->int myFib;
myFib := memoizedFib();

var int n;
read(n);
while n > 0 do
    print(myFib(n));
    read(n)
od;
