Aritmetica: potenza

Iterativa

MN = Pot(M, N) = M*M*...*M (N fattori)
Function PotIter(M, N: Integer): LongInt;
Var
   I, Risp: LongInt;
Begin
   Risp:=1;
   For I:=1 to N Do
      Risp:=Risp*M;
   PotIter:=Risp;
End;

Ricorsiva

  • M0 = 1, per N = 0
  • MN = M*MN-1, altrimenti

oppure

  • Pot(M, N) = 1, per N = 0
  • Pot(M, N) = M*Pot(M, N-1), altrimenti
Function PotRic(M, N: Integer): LongInt;
Begin
   If(N = 0) Then
      PotRic:=1
   Else
      PotRic:=M*PotRic(M, N-1);
End;