Iterativo
- M*N = 0, per N = 0
- M*N = M+M+...+M (N addendi), altrimenti
1 2 3 4 5 6 7 8 9 10 |
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, per N = 0
- M*N = INCREMENTAN(0, m), altrimenti
1 2 3 4 5 6 7 8 9 10 |
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; |
Oppure
- Prod(M, N) = 0, per 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
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
1 2 3 4 5 6 7 |
Function ProdRic(M, N: Integer): LongInt; Begin If(N = 0) Then ProdRic:=0 Else ProdRic:=ProdRic(M, N-1)+M; End; |