Earlier quoted context omitted.
Isn't this bit: const mid = Math.floor((right + left) / 2); susceptible to overflow? EDIT: Hm, perhaps not (in JS). Number.MAX_SAFE_INTEGER is much greater than I expected.
Not in Javascript, where everything is a double precision float. You would lose precision at about 2^51, but that’s not a limit that will meaningfully affect us for good while.
Ask HN: What's your favorite elegant/beautiful algorithm?
71–80 of 507 posts
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#72Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#73Binary search. Very simple, incredibly powerful; can search on data or math function. It's the basis for other CS concepts. JS implementation: https://gist.github.com/netgusto/90c8e0e7019a832cbf95eac58e1...
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#74An example discussion from google: https://jeremykun.com/2016/07/05/zero-knowledge-proofs-a-pri...
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#75Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#76You might have heard of "2nd order" or "4th order methods to calculate an integral. This means that the error drops off with the number of sampling points like N^-2 or N^-4, respectively. But Gaussian quadrature has spectral accuracy, which transcends this measure. Error goes down like an exponential of N.
Another neat feature is polynomials of degree 2N-1 or less are integrated exactly with this method. So if you have just 10 sampling points, you can calculate exactly the integral of a 19th order polynomial (times, perhaps, some known weighting function).
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#77Union-find data structure: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
You wanna flex your muscles with this data structure, this is a fun Project Euler problem: https://projecteuler.net/problem=186
Re: Ask HN: What's your favorite elegant/beautiful algorithm?
#78Binary search. Very simple, incredibly powerful; can search on data or math function. It's the basis for other CS concepts. JS implementation: https://gist.github.com/netgusto/90c8e0e7019a832cbf95eac58e1...
Isn't this bit: const mid = Math.floor((right + left) / 2); susceptible to overflow? EDIT: Hm, perhaps not (in JS). Number.MAX_SAFE_INTEGER is much greater than I expected.
const mid = Math.floor(left + (right - left) / 2);