Well, I don’t think it is the point of the article, but we might as well list the most beautiful algorithms we can think of, right?
Mergesort is quite beautiful I think. There’s something beautiful in the simplicity of just merging the sorted lists. Stable. No pathological cases. Nice access patterns.
Euclid’s algorithm [1] for finding the greatest common divisor of two numbers probably belongs on the list. As the name implies it was first described by Euclid, in 300 BC(!). It’s still widely used today.
I was looking for a word earlier that describes when it slowly dawns on a person how profound a thing is that’s been right in front of their eyes most of their life. The word I settled on was revelation, and one of the revelations I revisit as I grow older is the menacing terror presented by evolving lifeforms.
If you press the fast forward button as a theoretical “survivor in the game of life”, the creatures that were benign when they were static now become terrifying, constantly morphing organisms which gain and lose all manner of sharp and venomous appendages.
Like those early Deep Dream videos that always had creepy eyes and teeth everywhere on inanimate objects… that truly is what evolution looks like. It inspires instinctive fear of all the bad things, because that is exactly how it manifests.
When you release the fast forward button though and come back to the present, what was creepy and terrifying before suddenly becomes magnificent. A living representation of localized perfection.
Only in niches that get locked in an evolutionary arms race, as does sometimes happen but is not the norm. There’s also prokaryotic life out there chilling and doing the same thing it’s been doing for billions of years.
actually, it's interesting that not only does the oath not say that, but that doctors take all kinds of different oaths. They differ in all sorts of respects, and keep getting revised (like privacy policies).
Have you seen the movie Idiocracy? IMO it's a way more likely dystopia than 1984 or Brave New World, and it's explained here[1]. I would argue that that future is twisted and wrong, and is partly enabled by (mis)use of medicine.
Now I do think that nature's approach, while extremely robust, does result in a lot of collateral damage--e.g. the fit don't always survive (most of us know people who died young due to no genetic conditions and due to no fault of their own), and the unfit don't have to die, they just need to not reproduce.
It's certainly concise, but two post-evaluation assignments inside another assignment evaluated as a boolean condition feels more like the code equivalent of an Lovecraftian eldritch horror than a beautiful sunset.
(in de-Bruijn notation) which, given a list of input bits,
parses the encoding of an arbitrary algorithm (as a combinator over the minimal single point basis λxλyλz.x z (y (λt.z))) from this bit stream, and runs that algorithm on the remaining input.
Cool, speaking of rock stars, "You Give REST a Bad Name" is a bit heavier; since you're OP, maybe the next one could be "Is there a program that rocks as hard as...?"
I still remember a particular moment in the early 1980s. I was working through K&R, and temperature conversion (§1.2) had been mid, but the RPN calculator (§4.3) was lit, and having typed it in, then figured out its principles of operation, I just knew I wanted to become a programmer.
He heard one guitar, just blew him away
He saw stars in his eyes, and the very next day *
From then on, I was destined for a life of Hex, Bugs, and Rock&Roll.
Algorithms can certainly be elegant. A greedy algorithm with a lot of special cases added to force it to work feels yucky. A simple algorithm that does the minimal amount of work to get the job done and handles all the special cases without case-specific code feels good. Like a well-crafted, reliable tool. It might not be beauty yet, but it's close.
What makes it beautifull in my mind is the same thing that makes some poems beautifull - the fact that it seems simpler than the problem it solves, and yet it solves it well. If it evokes that surprising feeling of "That's it? That's enough?" - then it's beautifull, like a poem which expresses complex feeling in a few short verses.
On trees - how could we neglect the Succinct trees?
Succinctness - expressing trees with as little bits as entropically possible. The representation is as simple as it could be - simply write down nested parentheses whose abstract syntax tree is your given tree. Practically all tree operations in logarithmic or constant time. How it works - recursively with range-min trees and range-max trees - simple and elegant number theory.
Poetry involves a specific choice of words (perhaps with some thought to metre or rhyme), so if anything, we should expect programs to be as beautiful as poem.
(would there then be algorithms as beautiful as an argument, or functions as beautiful as an idea?)
Tedious, inobvious, trickish. Might do it for some people, but not for me. Double for-loop substring search tells so much more! Oh, pain, oh, obscenity.
The real beauty lies in the SLOW discrete Fourier transform. Such simplicity; but what’s really going on there? Highly recommend grokking DST for the transcendental beauty.
EDIT: note the different levels of abstraction: this is a program which implements a particular GCD algorithm, in a manner attempting to reflect the symmetry of the function which that algorithm calculates.
...Roger Hui: In the year 2033 Earth was discovered by Survey Fleet MCXII of the Galactic Empire. The Emperor ordered Earth to send a representative to the court, with strict instructions to “bring something beautiful”.
The following e-mail exchange took place on 2010-06-24 during a discussion on numeric representation.
Morten Kromberg: And… I shall fight against adding any form of NaN/Infinity — to the death! They will horribly complicate our implementation and don’t help users do anything useful.
Roger Hui: In the year 2033 Earth was discovered by Survey Fleet MCXII of the Galactic Empire. The Emperor ordered Earth to send a representative to the court, with strict instructions to “bring something beautiful”.
Emperor
What beautiful things does Earth have?
Earth Representative
Your excellency, our mathematician Euler proved in our year 1737 that +/(1+⍳∞)*-s ←→ ×/÷1-(⍭⍳∞)*-s
Emperor
What is the ⍭ symbol?
Earth Rep.
⍭i is the i-th prime.
Emperor
And what is ∞? Does it do anything useful?
Earth Rep.
It denotes infinity, your excellency.
Emperor
(ponders equation for a minute or two) Respect!
Emperor
Neat notation you have there. Tell me more.
Earth Rep.
Your excellency, it’s called APL. It was invented by the Canadian Kenneth E. Iverson …
I would say the combinator \x\y\z.x z (y (\t.z)) is more poetic than Y, since it forms a basis for combinatory logic all by itself. Not only can you make Y from it, but every other combinator as well. And it's the smallest term with that property.
Mergesort is quite beautiful I think. There’s something beautiful in the simplicity of just merging the sorted lists. Stable. No pathological cases. Nice access patterns.