PE1 Explained - kevinlawler/kona GitHub Wiki

Task

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Solution

  +/&~&/(!1e3)!/:3 5
233168

Explanation

1e3 is an abbreviation for 1000.0

(!1000)create a list of integers 0, 1, 2, 3, ... 999

! is mod (sometimes)

!/: is mod each-right

so (!1000)!/:3 5 is two lists, each of 0,...,999 mod 3, and each of 0,...,999 mod 5

& is min (sometimes — dyadic) and &/ is min over

so &/ ... is 0 if either of x mod 3 or x mod 5 was 0 (the min of two lists is the min of each of their items. this property holds for most verbs. it also works recursively)

~ is not, so all those zeroes we found become 1, everything else becomes 0

& is where (sometimes — min), it turns 1s into their index in the list, and 0 into nothing (try it out: what does where do on a list with numbers bigger than 1)

Plus sign is plus and +/ is plus over: sum everything together