The J video here - https://www.youtube.com/watch?v=OzaIi8BnHIc - walks you through building the J code for Project Euler problem 1. It ends with a variant that leads into Pascal's extremely terse version.
On Tue, Apr 8, 2014 at 11:56 AM, Pascal Jasmin <[email protected]>wrote: > 3 5 (0=|)"0 1 i.10 > 1 0 0 1 0 0 1 0 0 1 > 1 0 0 0 0 1 0 0 0 0 > > 3 5 (0 ([: +./ =) |"0 1) i.10 > 1 0 0 1 0 1 1 0 0 1 > > 3 5 (] #~ 0 ([: +./ =) |"0 1) i.10 > 0 3 5 6 9 > > +/ 3 5 (] #~ 0 ([: +./ =) |"0 1) i.1000 > 233168 > > > > ----- Original Message ----- > From: Jon Hough <[email protected]> > To: "[email protected]" <[email protected]> > Cc: > Sent: Tuesday, April 8, 2014 11:30:22 AM > Subject: [Jprogramming] Euler Project Problem 1 > > 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 > -- Devon McCormick, CFA ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
