Sie sind auf Seite 1von 1

FM(a, b, n) 1. if n = 1 then 2. if a = 0 or b = 0 then 3. return 0 4. if a = b then 5. return 1 6. else return 1 7. m := n 2 8. a := a div 2m 9. a := a rem 2m 10. b := b div 2m 11. b := b rem 2m 12.

c := F M (a , b , n m) 13. d := F M (a, b, m) 14. e := F M (a a, b b, m) 15. return c 22m + (c + d e) 2m + d The important dierence between the above algorithm and the one I gave in class is lines 8 11. These lines are not very dierent from before (in fact they are they same as before if a and b are positive). However, the above algorithm works dierently (from the one given in class) if a or b is negative. For example, if a = 7 and m = 2 then line 8 tells us that a := 7 div 4 = 1, and line 9 tells us that a := 7 rem 4 = 3. (this is dierent from the a and a we would get following the algorithm I gave in class). In general, when a is negative, line 9 above will tell us that we get a negative remainder. That turns out to be a useful property for the details that I told you to check. For the above algorithm we should still have the following: a = a 2m + a b = b 2m + b |a |, |b | < 2nm |a |, |b |, |a a |, |b b | < 2m which should allow us to prove correctness for the algorithm.

Das könnte Ihnen auch gefallen