Greatest common divisor: Difference between revisions

→‎{{header|SQL}}: Adds Postgres greatest common divisor function
(→‎=={{header|Visual Basic}}==: added Visual Basic example)
(→‎{{header|SQL}}: Adds Postgres greatest common divisor function)
Line 4,162:
DROP FUNCTION gcd;
</lang>
 
PostgreSQL function using a recursive common table expression
<lang SQL>CREATE FUNCTION gcd(integer, integer)
RETURNS integer
LANGUAGE sql
AS $function$
WITH RECURSIVE x (u, v) AS (
SELECT ABS($1), ABS($2)
UNION
SELECT v, u % v FROM x WHERE v > 0
)
SELECT min(u) FROM x;
$function$
</lang>
 
{{out}}
<pre>
postgres> select gcd(40902, 24140);
gcd
-----
34
SELECT 1
Time: 0.012s
</pre>
 
=={{header|Stata}}==
Anonymous user