SPOILER! My code is 5x slower than solutions. Why?

Please remember to use spoiler tags when asking questions about a working solution!

Since your code works, I would look closely at the solutions and see where they are different.

  1. Don’t store thing in an array if you only need them once. Building an array is expensive. Here you can just sum values as you find them.

  2. Don’t compute more than you need to. A number only has unique divisors up to the square root, s you shouldnt check numbers past the square root for divisability.

Also note, Javascript isn’t a high performance language, so timings and performance comparison can be a bit tricky. For more, see the discussion here:

1 Like