#!/usr/bin/octave -qf % Copyright 2005 Daniel Cer (daniel.cer@cs.colorado.edu) % % This work is licensed under the Creative Commons Attribution-NonCommercial- % ShareAlike License. To view a copy of this license, visit % http://creativecommons.org/licenses/by-nc-sa/2.5/ or send a letter to % Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, % 94105, USA. function fib=fibonacci(n) if n <= 0, fib=-1; return; endif prev_fib = 1; fib = 1; for i=3:n, next_prev_fib = fib; fib += prev_fib; prev_fib = next_prev_fib; endfor endfunction if nargin != 1, fprintf(stderr, "Usage:\n\t%s (n-th fibonacci # to calculate)\n", program_name); exit endif n = str2num(argv{1}); printf("fibonacci(%d): %d\n", n, fibonacci(n));