Yes - the idea of "a count variable while iterating through a loop" is very un-J-like. Though you can do something like this: ix=. _1 [ lim=. 1000 [ sum=. 0 while. lim>:ix=. >:ix do. sum=. sum+p: ix end.
why not just do what "Aai" suggested? On Sun, Apr 27, 2014 at 11:57 AM, Aai <[email protected]> wrote: > I think very J is: > > +/ i.&.(_1&p:) 1000000x > 37550402023 > > > > > On 27-04-14 17:36, Jon Hough wrote: > >> One problem I'm having with composing my own (non-trivial) J verbs is I >> am not sure how to hold onto a count variable when iterating through an >> array.For example: >> Problem: sum the number of primes less than 1000000. >> In Java-y syntax >> public int sumPrimes(){ int counter = 0; //my counter variable >> int max = 1000000; >> for(int i = 0; i < max; i++ ){ if(IsPrime(i)) counter >> += i; } return counter; } >> My first question is in J, how would I write a similar >> function/verb?Secondly, in that verb, how is "counter" stored as we iterate? >> (This question is not really about primes, but about holding onto >> variables through the iteration. I am aware of the p: and q: verbs) >> Regards,Jon >> ---------------------------------------------------------------------- >> For information about J forums see http://www.jsoftware.com/forums.htm >> > > -- > Met vriendelijke groet, > @@i = Arie Groeneveld > > > ---------------------------------------------------------------------- > 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
