News for hackers. Stuff that matters.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by jansan on Wednesday October 17, 2018 @10:03AM (#18239086)
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.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by DonaldPShimoda on Wednesday October 17, 2018 @02:13PM (#18241864)
> 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.
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by sblom on Wednesday October 17, 2018 @02:31PM (#18242117)
Isn't it a-b instead of a%b?
Re:Ask HN: What's your favorite elegant/beautiful algorithm? by bstamour on Wednesday October 17, 2018 @04:26PM (#18243446)
a % b is a way to do it in fewer iterations than a - b.