On Sun, Aug 28, 2022 at 1:38 PM Henry Rich <[email protected]> wrote:
> OK, I guess you could have a workaround.  The problem occurs only when
> there are no non-sparse entries, ...

Yes... for the algorithm I was working with, the "no sparse entries"
issue quickly disappears, so I was able to skip over that.

In other words, instead of

pritchard=: {{
  spokes=. $.,1
  primes=. i.0
  while. y > #spokes do.
    primes=. primes, p=. 2+(}.spokes) i.1 NB. find next prime
    rim=. #spokes NB. "length" of "circumference" of wheel
    spokes=. (y<.p*rim)$spokes NB. roll next larger wheel
    spokes=. 0 ((#~ y>])_1+p*1+i.rim)} spokes NB. remove newly
recognized prime from wheel
  end.
  while. y > p*p do.
    primes=. primes, p=. 2+(}.spokes) i.1 NB. find next prime
    spokes=. 0 ((#~ y>])_1+p*1+i.rim)} spokes NB. scrub it out of wheel
  end.
  primes,1+}.,I.spokes
}}

I went with

pritchard=: {{
  spokes=. $.6$4{.1
  primes=. 2, p=.3
  while. y > #spokes do.
    primes=. primes, p=. 2+(}.spokes) i.1 NB. find next prime
    rim=. #spokes NB. "length" of "circumference" of wheel
    spokes=. (y<.p*rim)$spokes NB. roll next larger wheel
    spokes=. 8 $.0 ((#~ y>])_1+p*1+i.rim)} spokes NB. remove newly
recognized prime from wheel
  end.
  while. y > p*p do.
    primes=. primes, p=. 2+(}.spokes) i.1 NB. find next prime
    spokes=. 0 ((#~ y>])_1+p*1+i.rim)} spokes NB. scrub it out of wheel
  end.
  primes,1+}.,I.spokes
}}

(on rosettacode...)

Thanks,

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to