1. Fakultet
Code Block | ||
---|---|---|
| ||
function y = fact(x) if x>1 y= fact(x-1)*x; else y=1; end end |
2. Fibonacci
Code Block | ||
---|---|---|
| ||
function f = fibnum(n) if n == 0 f = 0; elseif n == 1 f = 1; else f = fibnum(n-1) + fibnum(n-2); end end |
3. Pascals trekant
Code Block | ||
---|---|---|
| ||
function p = pascal(n) p = ones(1,n); if n <= 2 return end forrige_linje = pascal(n-1); for i = 2 : n-1 p(i) = forrige_linje(i-1) + forrige_linje(i); end end |
4. Palindrom
Code Block | ||
---|---|---|
| ||
function p = palindromTest(ord) if length(ord) == 1 || length(ord) == 0 p = 1; else if ord(1) == ord(length(ord)) && palindromTest(ord(2:(length(ord)-1))) p = 1; else p = 0; end end end |
5. Exponent
Code Block | ||
---|---|---|
| ||
function y = ten_exp(n) if n == 0 y = 1; else y = 10 * ten_exp(n-1); end end |