A pencil and paper solution.

A problem with computers is that it is too easy to go for a brute-force
solution. Computers are great for solving problems with no simple solutions
by applying brute-force, but this isn't one.

The problem with adding together all multiples of 3 and 5 in the series 0
... 999 is that multiples of 15 are counted twice. So instead of checking
each number to see if it is a multiple of 3 or 5, simply add together the
sum of all multiples of 3 and the sum of all multiples 5 then subtract the
sum of all multiples of 15. Easy to do on pencil and paper.

It is not necessary to actually sum the multiples as the sum is easily
calculated.

   E1=:4 : '+`-/x*-:(*>:)<.x%~<:y'

   3 5 15 E1 1000

233168


If we play with this we see a pattern.


   3 5 E1 10

23

   3 5 E1 100

2633

   3 5 E1 1000

266333

   3 5 E1 10000

2.66633e7

   3 5 E1 10000x

26663333

   3 5 E1 100000x

2666633333

   3 5 E1 1000000x

266666333333


Now look at a number impractical to solve by brute force on even the
biggest computers.


   googoi=:10x^100

   _50,\":3 5 E1 googoi

26666666666666666666666666666666666666666666666666

66666666666666666666666666666666666666666666666666

33333333333333333333333333333333333333333333333333

33333333333333333333333333333333333333333333333333
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to