Advertisement
rowanjacobson

Untitled

Nov 24th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #! This program computes the GCD of two positive integers
  2. #! using the Euclid method. Given a and b, a >= b, the
  3. #! Euclid method goes as follows: (1) dividing a by b yields
  4. #! a reminder c; (2) if c is zero, b is the GCD; (3) if c is
  5. #! no zero, b becomes a and c becomes c and go back to
  6. #! Step (1). This process will continue until c is zero.
  7.  
  8. Using Pseudo code:
  9.  
  10. Define a,b,c as Integers
  11. Input a ,b
  12.  
  13. IF (a < b) THEN #! since a >= b must be true, they
  14. c = a #! are swapped if a < b
  15. a = b
  16. b = c
  17. END IF
  18.  
  19. #!using Euclid's method we need to loop through the following
  20. DO
  21. c = MOD(a, b) #! compute c, the reminder
  22. IF (c == 0) EXIT #! if c is zero, we are done. GCD = b
  23. a = b #! otherwise, b becomes a
  24. b = c #! and c becomes b
  25. END DO #! go back
  26.  
  27. Print('The GCD is ', b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement