Extremely simple one, but my favorite is an algorithm for determining if two words are anagrams of each other: The Fundamental Theorem of Arithmetic states: "every integer greater than 1 either is a prime number itself or can be represented as the product of prime numbers and that, moreover, this representation is unique, up to (except for) the order of the factors."[1] So to determine that two words are anagrams of…
Ask HN: What's your favorite elegant/beautiful algorithm?
421–430 of 507 posts
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#422Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#423Xor Swap.... https://en.wikipedia.org/wiki/XOR_swap_algorithm not because it's super practical ( and there are other variations using other operators ) I like it because it is super simple and when I first encountered it early on in my learning, it was really not immediately obvious why it worked. It's probably been the simplest piece of code that's surprised me.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#424The rsync algorithm. There is a theorem that it is impossible in general to remotely compare two files with less network traffic than it requires to simply send one file over the wire. But rsync does it. How? By accepting a theoretical but unlikely possibility of coming up with the wrong answer. What rsync does is compare hashes of ranges of stuff. If the hash comes out the same, the two are assumed to be identical w…
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#425There is a javascript implementation [0] which is where I saw it the first time, it is about 500 lines of fully commented code. The algorithm is so simple, I absolutely love it.
Basically, when performing a union on two meshes, we determine the 3d BSP trees from both meshes, then simply clip each mesh using the other's tree. The output is the union of the two leftover meshes.
And since both intersection and difference can be written as a combination of inversion and union, they are simply composed from those.
I can't find a good paper on the algorithm though, not sure where exactly it originated.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#426Earlier quoted context omitted.
`git bisect` is an indispensible git trick. You give it commit where you know the bug exists, and one where you know it doesn't and it'll use binary search to help you find where the bug came from. It's absolutely wonderful.
I always struggle to see why people bang on about git bisect. You need tests to make it work. If you have tests, why aren't you running them continuously? If you're running them continuously, why do you need git bisect?
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#427Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#428Xor Swap.... https://en.wikipedia.org/wiki/XOR_swap_algorithm not because it's super practical ( and there are other variations using other operators ) I like it because it is super simple and when I first encountered it early on in my learning, it was really not immediately obvious why it worked. It's probably been the simplest piece of code that's surprised me.
I was asked this in an interview. I was a bit pissed
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#429Fisher-Yates shuffle. An elegant and surprisingly intuitive O(n) algorithm that doesn't take up extra memory.
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#430Earlier quoted context omitted.
Interesting! I knew that med school applicants were accepted on some sort of ranking and matching process but didn't realize there was a real algorithm behind it. This, or one similar, seems to be what's behind it.
There's no matching process at the beginning of medical school, but there is for assigning med school graduates to residencies. This variant of the problem is NP-hard, so there's no exact solution, but matching still works pretty well. https://web.stanford.edu/~alroth/papers/rothperansonaer.PDF has all the details.