... outer product ! ...
On 03/01/2019 08:57, 'Mike Day' via Chat wrote:
Thanks, Roger.
I was going to say that I avoid each and inner product, thanks to
trying/doing too many
Project Euler problems where the APL/J one-liner approach tends to be
a false
friend when scaling up to the challenge, although often fine for the
training
example/s.
However, our two variants, rrr and f run each other pretty close in
performance
even for reasonably large n:
ts'#f 100000000'
3.95857 1.42073e9
ts'#rrr 100000000'
4.09369 8.39386e8
timer'(rrr-:f) 100000000' NB. using my own crude one-pass timing fn!
+-------+-+
|8.07301|1|
+-------+-+
BTW, I still run foul of precision in those Project Euler problems I
can still solve -
they're often very difficult (for me!) these days - eg I resorted to
Pari GP for
problem 647 (a real prime!), one of the easiest recent ones, as being
quicker than
working out where the precision was failing in the slightly more
elegant J code!
Cheers,
Mike
On 03/01/2019 00:12, Roger Hui 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
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm