Same idea as Mike Day's functions, but using straight-line code
(non-looping, non-recursive, non-iterative):
f=: 3 : 0
p=. i.&.(_1&p:) -:>:y
q=. p {.~ p I. %:y
/:~ ; q *&.> (1+i.#q) }.&.> (p I. 1+<.y%q) {.&.> <p
)
*./ (f -: rrr)"0 i. 10000
1
FYI: "The idea": If a number n has exactly 4 distinct divisors, then those
divisors are 1,q,p,n where q and p are distinct primes and n=q*p. To get
the list of such numbers n, the function f exploits the fact that q can be
bounded by sqrt n.
On Tue, Jan 1, 2019 at 8:31 AM 'Mike Day' via Chat <[email protected]>
wrote:
> Somewhat more correct than my earlier offering, ppp:
>
> rrr =: 3 : 0
> n =. y
> p =. 1, p: i. >: _1 p: -:n
> l =. ''
> while. #p =. }. p do.
> l =. l, ({.*}.) p =. (([{.~ I.+e.~)n%{.) p
> end.
> /:~ l NB. sort list
> )
>
> sss =: 3 : 0 NB. Same idea as a non-tacit one-liner...
> /:~;({.*}.)each }. (([{.~ I.+e.~)n%{.)@}.each^:a:<1 , p: i. >: _1 p: -:n
> =. y
> )
>
> _10{. "1 (ppp, rrr,: sss) 2025. NB. ppp missed a near prime year!
> 1981 1982 1983 1985 1991 1994 2005 2018 2019 0
> 1981 1982 1983 1985 1991 1994 2005 2018 2019 2021
> 1981 1982 1983 1985 1991 1994 2005 2018 2019 2021
>
> Note that sss uses more time and space than ppp and rrr.
>
> Cheers,
> Mike
>
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm