Hacker News new | past | comments | ask | show | jobs | submit login

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.



> In my opinion this is the greatest one-liner ever.

I dunno, I really like Haskell's one-line infinite Fibonacci sequence:

    > let fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
    > take 7 fibs
    [1,1,2,3,5,8,13]
    > fibs !! 1000
    70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501
I don't think it's the most efficient solution, but it really showcases lazy evaluation with a simple example.


Isn't it a-b instead of a%b?


a % b is a way to do it in fewer iterations than a - b.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: