The Euclidean algorithm to compute the greatest common divisor of two numbers.
In Javascript, if you have two numbers x and y that are both greater than zero, you compute the greatest common divisor xyGcd in one beautiful line of code:
var xyGcd = function gcd(a,b){ return b ? gcd(b, a%b) : a; }(x, y);
In my opinion this is the greatest one-liner ever.
Comments (491) 209 threads
Show all comments
Showing thread 14 of 209
In Javascript, if you have two numbers x and y that are both greater than zero, you compute the greatest common divisor xyGcd in one beautiful line of code:
In my opinion this is the greatest one-liner ever.I dunno, I really like Haskell's one-line infinite Fibonacci sequence:
I don't think it's the most efficient solution, but it really showcases lazy evaluation with a simple example.