"Get the sum of primes less than a given number".

Note that there are at least three completely independent concepts that
this could be referring to.

Let's say our number is 30.

Possibility 1: We want the sum to be less than 30.
Possibility 1a: 28 NB. +/2 3 5 7 11
Possibility 1b: 29 NB. a single prime whose value is less than 30

Possibility 2: We want to sum the primes less than 30.
   +/ i.&.(p:inv) 30
129

Let's assume you are trying to solve the easy problem (possibility 2). How
did that work?

   p:inv 30
10

This tells us how many prime numbers there are which are not larger than 30.

   i. p:inv 30
0 1 2 3 4 5 6 7 8 9

This gives us the counting integers which are less than 10.

   p: i. p:inv 30
2 3 5 7 11 13 17 19 23 29

This gives us the primes which are less than 30

Now all we have to do is sum them.

Ric Sherlock has answered that already.

Thanks,

-- 
Raul



On Tue, Apr 29, 2014 at 11: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