Comment

Comment by delibes

Ah, memories. Back in 2010 I whinged to colleagues who were using their fancy functional languages to solve problem 1 inefficiently in 2 lines of code...

" Project Euler problem 1 :-

It helps to know the sum of (1 .. x) is 0.5 * n * (n+1).

Multiples of 3 less than 1000 are (3 .. 999) == (1 .. 333) * 3 = 0.5 * 333 * 334 * 3 = 166833

Multiples of 5 less than 1000 are (5 .. 995) == (1 .. 199) * 5 = 0.5 * 199 * 200 * 5 = 99500

We must subtract common multiples (those of 15), so (15 .. 990) = (1 .. 66) * 15 = 0.5 * 66 * 67 * 15 = -33165

166833 + 99500 - 33165 = 233168

O(1) solution FTW :P Nice looking code is good, but a smart algorithm makes a big difference. "

OK, I was a smug arsehole, but there's a point to be made y'know?

parent delibes 9 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton