function f = fattoriale(n) f = 1; for ii = 2 : n f = f * ii; n! n > 0 n! = n n 1 n 2 2 1
fattoriale(2) ans = 2 function f = fattoriale(n) f = 1; for ii = 2 : n f = f * ii;
k = 2; f2 = fattoriale(k); function f = fattoriale(n) f = 1; for ii = 2 : n f = f * ii; k f2 f ii n f ii n k f2
n! = n n 1! n! = n n 1 n 2 2 1 n 1!
https://en.wikipedia.org/wiki/airplane!
fatric(3) fatric(2) fatric(1) fatric(0)
f(n) = n! = n (n 1) (n 2) 3 2 1 f(0) = 1 f(n) = n f n 1
function [f] = factric(n) if (n == 0) f = 1; f = n * factric(n - 1); 0! = 1 n! = n n 1!
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3)
function [f] = factric(n) if (n == 0) f = 1; f = n*factric(n-1); factric(3) ans = 6
function [f] = factric(n) if (n == 0) f = 1; f = n * factric(n-1);
function [f] = factric(n) f = n * factric(n-1);
function [f] = factric(n) if (n == 0) f = 1; f = n * factric(n-1);
function [f] = factric(n) if (n == 0) f = 1; f = n * factric(n);
>> factric(600) Out of memory. The likely cause is an infinite recursion within the program. Error in fatric at line XXX
function somma = sommanumericompresi(a, b) if a == b somma = a; disp(['caso base somma =,... num2str(somma)]); disp(['prima chiamata =, num2str(a)]); somma = a + sommanumericompresi(a+1, b); disp(['dopo chiamata =, num2str(a)]);
function somma = sommanumericompresi(a,b) if b < a somma = 0; if a == b somma = a; disp(['caso base somma = ', num2str(somma)]); disp(['prima chiamata a = ',num2str(a)]); somma = a + sommanumericompresi(a+1, b); disp(['dopo chiamata a = ',num2str(a)]);
function somma = sommanumericompresi(a,b) if b < a somma = 0; return; if a == b somma = a; disp(['caso base somma = ', num2str(somma)]); disp(['prima chiamata a = ',num2str(a)]); somma = a + sommanumericompresi(a+1, b); disp(['dopo chiamata a = ',num2str(a)]);
function somma=calcolasommacompresi2(a_temp,b_temp) a = min([a_temp,b_temp]); b = max([a_temp,b_temp]); if(a==b) disp(['caso base 1 a=',num2str(a),'b=',num2str(b)]) somma = a; if(b-a == 1) disp(['caso base 2 a=',num2str(a),'b=',num2str(b)]) somma = a + b; disp(['prima della chiamata ricorsiva a=',num2str(a),'b=',num2str(b)]) somma=a + calcolasommacompresi2(a+1,b-1) + b; disp(['dopo la chiamata ricorsiva a=',num2str(a),'b=',num2str(b),' somma =',num2str(somma)])
n f N
function gg = stagno(n, f, N) if (n >= N) % caso base: lo stagno e pieno gg = 0; % chiamata ricorsiva: non abbiamo N ninfee % il numero di giorni necessari per riempire lo stagno è uguale a un giorno + il numero di giorni che saranno necessari domani (quando le ninfee saranno diventate n * f). gg = 1 + stagno(n * f, f, N);
function [giorni, vettore_n] = ninfee(n, f, N) if (n >= N) % caso base giorni = 0; vettore_n = []; fprintf('\n caso base oggi ho %2.2f ninfee', n); ndomani = round(n * f); % controllo convergenza if ndomani == n giorni = nan; fprintf('\ncrescita o popolazione iniziale insufficiente'); return % chiamata ricorsiva fprintf('\n oggi ho %2.2f ninfee', n); % la popolazione aumenta [giorni, vettore_n] = ninfee(ndomani, f, N); giorni = giorni + 1; vettore_n = [ndomani, vettore_n];
function s = calcolalunghezza(str) if(isempty(str)) s = 0; s = 1 + calcolalunghezza(str(2 : ));
function stampaalcontrario(frase) % caso base if isempty(frase) % return % chiamata ricorsiva disp(frase()); % stampa al contrario stampaalcontrario(frase(1:-1))
function stampaalcontrario(frase) % caso base if isempty(frase) % return % chiamata ricorsiva stampaalcontrario(frase(1:-1)) disp(frase()); % stampa dritto
function stampa(frase) % caso base if isempty(frase) % return % chiamata ricorsiva stampa(frase(2:)) disp(frase(1));
function stampa(vettore) % caso base if (length(vettore) == 1) fprintf('%c, vettore(1)); fprintf('%c, vettore(1)); % chiamata ricorsiva fprintf('%c, vettore(1)); stampa(vettore(2:)); fprintf('%c, vettore(1));
function stampaalcontrario(str) if(isempty(str)) disp(str()); stampaalcontrario(str(1 : -1)); disp(str()); >> stampaalcontrario('ciao') o a i c c i a o
function stampaalcontrario(str) if(isempty(str)) disp(str()); stampaalcontrario(str(2 : )); disp(str()); >> stampaalcontrario('ciao') o o o o o o o o
function stampaalcontrario(str) if(isempty(str)) disp(str(1)); stampaalcontrario(str(2 : )); disp(str(1)); >> stampaalcontrario('ciao ) c i a o o a i c
function stampaalcontrario(str) if(isempty(str)) disp(str(1)); stampaalcontrario(str(1 : -1)); disp(str(1)); >> stampaalcontrario('ciao ) c c c c c c c c
function vet = chefa(vet) if isempty(vet) length(vet) == 1 return; if all(vet(1) >= vet(2 : )) return; vet(vet == vet(1)) = []; vet = chefa(vet); >> chefa([19 18 12 19 12 3]) >> chefa([7 18 13 19 12 3]) >> chefa([ 2 3 18 12 19 12 3])
>> chefa([ 19 18 12 19 12 3]) ans = 19 18 12 19 12 3 >> chefa([7 18 13 19 12 3]) ans = 19 12 3 >> chefa([ 2 3 18 12 19 12 3]) ans = 19
all(vet(1) > vet(2 : )) >> chefa([ 19 18 12 19 12 3]) ans = 18 12 12 3
30 18 12 18 12 6 6 6
function [M] = MCDeuclidRic(m,n) if m == n M = m; if m > n M = MCDeuclidRic(m-n, n); M = MCDeuclidRic(m, n-m);
function [fib] = FiboRic(n) if n == 1 n == 2 fib = 1; fib = FiboRic(n-2) + FiboRic(n-1);
function [fl]=fiblist(n) fl(1) = 1; fl(2) = 1; for k = 3:n fl(k) = fl(k-2) + fl(k-1);
N N
function [res] = controllapalindromo(stringa) % stringhe di un carattere (o vuote sono palindrome) if length(stringa) < 2 res = true; % controllo se gli estremi sono uguali if stringa(1)==stringa() % in tal caso richiama controllapalindromo su stringa(2, -1) res=controllapalindromo(stringa(2:-1)); res=false;
function [res, esp] = ispotenzadi(num, base) % caso base if num == base res = 1; esp = 1; if(mod(num, base) == 0) % esp = esp + 1; % da errore [res, esp] = ispotenzadi(num/base, base); esp = esp + 1; res = 0; esp = NaN;
function [res, esp] = ispotenzadi(num, base) % caso base if(num == base) res = 1; esp = 1; if(num < base) res = 0; esp = NaN; % interrompo se n non è intero if(round(num) ~= num) res = 0; esp = NaN; % chiamata ricorsiva [ris, esp] = ispotenzadi(num/base, base); esp = esp +1; res = controllasepotenza(n, d*d); d^2 d^4
function r = cosafa(array) k = size(array, 2); if (k == 1) r = 1; if (k == 2) if (array(1) + array(2) == 10) r = 1; r = 0; if (array(1) + array(k) == 10) r = cosafa(array(2:k-1)); r = 0;
function [ris] = mistero(v, n) if (n > 1) v2 = v(mod(v, n) ~= 0 v == n); ris = mistero(v2, n-1); ris = v; x x = mistero([2 3 4 5 7 9 11 13 15], 10) x = 2 3 5 7 11 13
f a, b f a f b 0 c a, b f c = 0 haunozero
calcolazeri f a, b f x == 0 f x < ε ε