How to shorten this J code? (Or write shorter code in another language) I decided to learn J so that I could better take part in code golfing. Starting it off relatively simple, I tried writing a solution to this problem:


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


After a bit of tinkering, I came up with the following J code that solves it:

x=:i.1000
+/x#(0=3|x)+.0=5|x


Basically, what I'm doing is taking two lists of numbers from 1 to 1000, and finding one modulo 3, and the other modulo 5. Then I OR them together to get a list of indicies where a number is divisible by either 3 or 5. I then use that to sum the numbers.

What I ask is, how do I shorten this? I'm new to J and so do not have enough experience to shorten it any further.

Also, if anyone has a solution shorter than this in another language, I'd be interested to see that as well!
Employing a few forks, a hook and the fact that J works well on lists:

+/(#~[:+./0=3 5|/])i.1000


This answer uses your algorithm, but just uses some J tricks to make it a bit shorter. I'm fairly sure that this can be made shorter still, and I wouldn't be surprised if there's a slightly different way of doing it that results in an even shorter solution.

Here's a list of some things I've done here.

First, I've combined the 3|x and the 5|x into one operation 3 5|/x. This uses the table adverb / to do both operations at once:

   3 5|/i.20
0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2 0 1
0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4


Next, I've combined the |/, the 0=, the +., and the # into a train of verbs using forks and a hook. The verb train breaks down into groups of three:

3 5|/]


this is the same as the above, except I'm using ] to grab the list to the right of the brackets.

0=x


x is the result of the previous fork which is fed into the next.

[:+./x


as before the result so far gets fed into this fork. [: tells the verb +./ that it should use its monadic (one argument) form. The / inserts the +. between the top and bottom rows of the result so far.

#~x


This is the hook. The result so far is fed in as the right argument, but the left argument is the value outside the brackets (i.1000) again. So this is equivalent to (i.1000)#~x. The ~ just reverses the order of the arguments.

Then at the end I just use the +/ as you did.