Posted By

Scooter on 05/24/08


Tagged

math


Versions (?)

Euclid's Algorithm


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/05/euclids-algorithm.html

  1. <%
  2. ' ASP Mathematics Library - Euclid's Algorithm
  3. '
  4. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  5. '
  6. ' This work is licensed under the Creative Commons Attribution License. To view
  7. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  8. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  9. ' 94305, USA.
  10.  
  11. ' Determine the greatest common divisor of two numbers using Euclid's algorithm.
  12. function gcd(byVal a, byVal b)
  13. a = abs(a)
  14. b = abs(b)
  15.  
  16. if a = 0 then
  17. gcd = b
  18. elseif b = 0 then
  19. gcd = a
  20. elseif a > b then
  21. gcd = gcd(b, a mod b)
  22. else
  23. gcd = gcd(a, b mod a)
  24. end if
  25. end function
  26.  
  27. ' Determine the least common multiple of two numbers using Euclid's algorithm.
  28. function lcm(byVal a, byVal b)
  29. a = abs(a)
  30. b = abs(b)
  31.  
  32. if a > b then
  33. lcm = (b / gcd(a, b)) * a
  34. else
  35. lcm = (a / gcd(a, b)) * b
  36. end if
  37. end function
  38. %>

Report this snippet  

You need to login to post a comment.