[Haskell-beginners] Project Euler #01 on HackerRank, Performance issue‏

Frerich Raabe raabe at froglogic.com
Wed Jan 28 11:12:50 UTC 2015


On 2015-01-28 00:57, Jean Lopes wrote:
> I'm not really good at math, maybe I am missing something obvious ?
> Maybe some pointers as of where to start studying math in order to avoid 
> this brute force attempts, at least to help in this particular
> problem

I'm not too familiar with 'Project Euler', but summing all multiples of 3 and 
5 below some given 'n' made me think: e.g. 16 it
would be...

     3 + 5 + 6 + 9 + 10 + 12 + 15
   = 3 + 6 + 9 + 12 + 15 + 5 + 10                    | reordered summands
   = 3 + 6 + 9 + 12 + 15 + 5 + 10 + 15 - 15          | +15-15 appended to 
prepare factoring out
   = 3 + 6 + 9 + 12 + 15  + 5 + 10 + 15  - 15        | whitespace for 
readability
   = 3 * (1+2+3+4+5)      + 5 * (1+2+3)  - 15 * (1)  | factoring out

Now I remembered a trick to get the sum of the first N numbers (I only 
remember it's called "der kleine Gauß" in german):

   1 + 2 + ... + n = (n^2 + n) / 2

Maybe that'll help.

- Frerich



More information about the Beginners mailing list