Advertisement
Fhernd

ExponenciacionEficiente.gcl

Oct 15th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.28 KB | None | 0 0
  1. [Ctx C: a, b: nat & b>=0
  2.     {Q: T}
  3.  
  4.     x, y, z := a, b, 1;
  5.    
  6.     {Inv P: y>=0 & z*x^y = a^b}
  7.    
  8.     {cota t: y}
  9.    
  10.     do y > 1 ->
  11.         if y mod 2 = 0 ->
  12.             x, y := x * x, y / 2;
  13.         [] y mod 2 != 0 ->
  14.             z, x, y := z * x, x * x, (y - 1) / 2;
  15.         fi
  16.     od
  17.    
  18.     z = x * y;
  19.    
  20.     {R: z = a^b}
  21. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement