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

Reply via email to