The issue with getPrime is that @. doesn't like working with arrays. To fix
it you'd need to add "0 to the end i.e.
getPrime=: (*&0)`(*&1)@.(1&p:)"0

Feeding the verb one atom at a time is likely to reduce performance for
large arrays. The following produces a similar output to the one above but
operates on the whole of the argument given as y
getPrime1=: (* 1&p:)

Depending on what you're trying to do this may be preferable:
getPrime2=: (#~ 1&p:)





On Wed, Apr 30, 2014 at 3:22 AM, Jon Hough <[email protected]> wrote:

> This question comes in two parts.
> Statement of problem I am trying to solve:
> Get the sum of primes less than a given number.
> Question (1):What is the best way to do this?
> What I am attempting now is to use gerunds and the p: verb.Essentially I
> want to add all the numbers in an array if they are primes.
> This is what I have so far:
> getPrime=. (*&0)`(*&1)@.(1&p:) NB. If not prime returns 0, if prime
> returns original number.
> I will then +/ through the returned array.getPrime works for a single
> number. But whenever I try to use a 1-d array as the noun I get a "rank
> error".I am not sure how to fix this.
> Question (2): How do I fix the above error? Even if my method is not a
> very good way to solve the original problem I would still like to know why
> I doesn't work.
> Regards.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to