Euclid Rides Again… and Again!

David Fetter wrote a post about how to find GCD of two numbers. In his post David call it “a quick way”. 🙂 But let me disagree. This is more efficient way:

 
WITH RECURSIVE t(a, b) AS (
	VALUES (38, 12)
UNION ALL
	SELECT b, mod(a,b) FROM t
	WHERE b > 0
)
SELECT a AS gcd FROM t WHERE b = 0;

Cheers!

Advertisements

2 thoughts on “Euclid Rides Again… and Again!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s