This project is read-only.
Recursive, naive Fibonacci sequence:
# fib (1 -> 1)
# Calculates the nth Fibonacci sequence number.
fib: {
    # If n=0, do nothing. fib(0) = 0
    0 =? { } {
        # If n=1, do nothing. fib(1) = 1
        1 =? { } {
            1 sub #(n-1)
            dup 1 sub #(n-1) (n-2)
            fib #(n-1) fib(n-2)
            swap fib #fib(n-2) fib(n-1)
            add #(fib(n-2) + fib(n-1))
        }
    }
}

main: {
    # Print the 11th Fibonacci sequence number.
    11 fib printint
}


Iterative Fibonacci sequence:
# fib (1 -> 1)
# Calculates the nth Fibonacci sequence number.
fib: {
    # If n=0, do nothing. fib(0) = 0
    0 =? { } {
        # If n=1, do nothing. fib(1) = 1
        1 =? { } {
            0 1 3 brot
            1 sub times {
                dup 3 brot add
            }
        }
    }
}

main: {
    # Print the 11th Fibonacci sequence number.
    11 fib printint
}

Last edited May 16, 2012 at 5:47 AM by jdmichal, version 3

Comments

No comments yet.