Hey Jon, I remember seeing this done by someone before. They went about it like this: generate numbers 0 through 1000 a = i.1000 create a "mask" list of 0s and 1s where the numbers are divisible by 3 and 5 <insert J statement that does this...> (pseudo code: b = a % 3 OR a % 5) multiply the mask list and the number list together and add all the numbers c = +/(a * b)
Excuse me for my bad J :) I keep learning parts of it but then end up not using it for weeks... Enjoy, Lee On Tue, Apr 8, 2014 at 11:30 AM, Jon Hough <[email protected]> wrote: > Euler Project #1 is: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. > In C/Java I could easily do something like: > int total =0; > for (int counter = 0; counter < 1000; counter ++ ){if(counter %3 = 0 || > counter %5 = 0){ total += counter;}} > return counter; > > The main features here being the if statement and the for-loop. > Obviously in J things need to be done differently. Here is my first attempt: > div3 =. 0&(="0)@:( 3&|) NB. is residue zerodiv5 =. 0&(="0)@:( 5&|) > div3or5 =. div3"0 +."0 div5"0 > dummy =. 1&*"0 NB. You'll see why I need this... > result =. dummy * div3or5 > NB. final result is: > +/result i.1000 > > I used dummy because I wanted to do something like multiply the value of y > by "div3or5 y" so only multiples of 3 or 5 would be nonzero. > Before attempting this problem I assumed it could be done in only one line > and am pretty unhappy by my long solution. Any help explaining where I have > gone wrong, or why my attempt at a fork is silly would be appreciated. > Thanks in advance, > Jon > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
