How to resolve the algorithm Greatest common divisor step by step in the MySQL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Greatest common divisor step by step in the MySQL programming language
Table of Contents
Problem Statement
Find the greatest common divisor (GCD) of two integers.
Greatest common divisor is also known as greatest common factor (gcf) and greatest common measure.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Greatest common divisor step by step in the MySQL programming language
Source code in the mysql programming language
DROP FUNCTION IF EXISTS gcd;
DELIMITER |
CREATE FUNCTION gcd(x INT, y INT)
RETURNS INT
BEGIN
SET @dividend=GREATEST(ABS(x),ABS(y));
SET @divisor=LEAST(ABS(x),ABS(y));
IF @divisor=0 THEN
RETURN @dividend;
END IF;
SET @gcd=NULL;
SELECT gcd INTO @gcd FROM
(SELECT @tmp:=@dividend,
@dividend:=@divisor AS gcd,
@divisor:=@tmp % @divisor AS remainder
FROM mysql.help_relation WHERE @divisor>0) AS x
WHERE remainder=0;
RETURN @gcd;
END;|
DELIMITER ;
SELECT gcd(12345, 9876);
You may also check:How to resolve the algorithm Mutual recursion step by step in the PARI/GP programming language
You may also check:How to resolve the algorithm Regular expressions step by step in the 8th programming language
You may also check:How to resolve the algorithm Jordan-Pólya numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Rock-paper-scissors step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Tree traversal step by step in the APL programming language