|
Soluzione iterativaM*N = 0 N = 0 M*N = M+M+...+M (N addendi) altrimenti Codifica function ProdIter(M, N: Integer): LongInt; Var I: Integer; Risp: LongInt; begin Risp:=0; for I:=1 to N do Risp:=Risp+M; ProdIter:=Risp; end; oppure M*N = 0 N = 0 M*N = IncN(0, M) altrimenti Codifica function ProdIter(M, N: Integer): LongInt; Var I: Integer; Risp: LongInt; begin Risp:=0; for I:=1 to N do Inc(Risp, M); ProdIter:=Risp; end; Soluzione ricorsivaProd(M, N) = 0 N = 0 Prod(M, N) = Prod(M, N-1)+M altrimenti Esempio: M=5, N=3 Prod(5, 3) = Prod(5, 3-1)+5 = Prod(5, 2)+5 = 10+5 = 15 Prod(5, 2) = Prod(5, 2-1)+5 = Prod(5, 1)+5 = 5+5 = 10 Prod(5, 1) = Prod(5, 1-1)+5 = Prod(5, 0)+5 = 0+5 = 5 Prod(5, 0) = 0 Codifica function ProdRic(M, N: Integer): LongInt; begin if(N = 0) then ProdRic:=0 else ProdRic:=ProdRic(M, N-1)+M; end; |
|