Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

a)

Code Block
function fn = fibonacci(n)
if n < 3
    fn = 1;
else
    fn = fibonacci (n-2)+fibonacci (n-1);
end

b)

Code Block
function fibStore(n,filename)
fib = fibonacci(n);
fhandle = fopen(filename,'w');
for element = fib
    fprintf(fhandle,'%d\n',element);
end
end

function fn = fibonacci(n)
fn = ones(1,n);
if n > 2
    a = fibonacci(n-1);
    fn(1:n-1) = a;
    fn(n) = a(end-1) + a(end);
end
end

c)

Code Block
function ret = fact(n)
if n > 2
    ret = n*fact(n-1);
else 
    ret = 2;
end
end

d)

Code Block
function r = des2bin (n)
if n == 0
    r = '0';
elseif n == 1
    r = '1';
else
    rest = rem (n ,2) ;
    if rest == 0
        r = [ des2bin ( n/2 ) '0'];
    else
        r = [ des2bin ( (n -1) /2 ) '1'];
    end
end
end

e)

Code Block
function tower_of_hanoi (n, source , dest , temp )
if n > 0
    tower_of_hanoi (n -1, source , temp , dest );
    fprintf ('Flytt fra %i til %i\n', source , dest );
    tower_of_hanoi (n -1, temp , dest , source );
end
end