And, to generate the list of semiprimes up to n, change the 1+i.#q in f to
i.#q, thus:
sp=: 3 : 0
p=. i.&.(_1&p:) -:>:y
q=. p {.~ p I. %:y
/:~ ; q *&.> (i.#q) }.&.> (p I. >.y%q) {.&.> <p
)
(sp -. f) 1000
4 9 25 49 121 169 289 361 529 841 961
%: (sp -. f) 1000
2 3 5 7 11 13 17 19 23 29 31
On Wed, Jan 2, 2019 at 6:00 PM Roger Hui <[email protected]> wrote:
> FYI: A *semiprime* <http://en.wikipedia.org/wiki/Semiprime> is a number
> that is the product of two (not necessarily distinct) prime numbers.
> This is nearly the same as what we have been computing, but here we require
> that the two prime numbers be unequal.
>
>
> On Wed, Jan 2, 2019 at 4:12 PM Roger Hui <[email protected]>
> wrote:
>
>> 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