[Jprogramming] Problem using Tie
Ric Sherlock
tikkanz at gmail.com
Tue Apr 29 16:21:14 UTC 2014
I like the ( p: i. p:^:_1 y ) technique. That will certainly be an
improvement on creating an array of all numbers less than the number in
question.
Here is a tacit version (a bit messier than the explicit one)
sumPrimes=: +/@:p:@i.@(p:inv)
On Wed, Apr 30, 2014 at 3:56 AM, Don Guinn <donguinn at gmail.com> wrote:
> The choice as to which part of the gerund chooses has to be made for each
> number. Use rank to let it process each number separatery.
>
> getPrime=. ((*&0)`(*&1)@.(1&p:))"0 NB. If not prime returns 0, if prime
> returns original number.
> getPrime 17 18 19
> 17 0 19
>
> "Best" is a judgement and depends on the situation and person making it. I
> like:
>
> sum_primes=:3 : '+/p:i.p:^:_1 y'
> sum_primes 20
> 77
>
>
> On Tue, Apr 29, 2014 at 9:22 AM, Jon Hough <jghough at outlook.com> 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
>
More information about the Programming
mailing list