Greatest Common Denominator

Developed In: SQL — Contributed by: Scott Noyes

Find the greatest common factor of two positive integers.


Scott Noyes
SQL
  1. CREATE FUNCTION gcd(x int, y int) RETURNS int DETERMINISTIC
  2. BEGIN
  3. DECLARE dividend int;
  4. DECLARE divisor int;
  5. DECLARE remainder int;
  6. SET dividend := GREATEST(x, y);
  7. SET remainder := LEAST(x, y);
  8.  
  9. WHILE remainder != 0 DO
  10. SET divisor = remainder;
  11. SET remainder = MOD(dividend, divisor);
  12. SET dividend = divisor;
  13. END WHILE;
  14.  
  15. RETURN divisor;
  16. END

You must be logged in to tag this tool

No Comments yet

Votes

Not yet rated.
You must be logged in to vote.

Watches

0 members are watching this tool
You must be logged in to track this tool.

Provide Feedback

Please note:
HTML will be purified, but we allow for a number of HTML tags so that you have the flexibility to decorate your comment text to some extent. The comments allow the following HTML tags:

strong, b, em, blockquote, a, code, pre

To put code into your comment, simply encapsulate your code with
[code language="XXX"][/code], where XXX is any common language, for instance "PHP", "SQL", "C", etc.



You must be logged in to comment