Bo beat me to it!
FWIW, here's a slightly different way to do this if you avoid
or don't know about +. or the + key is broken! -
with a cursory examination of performance:
ts =: 6!:2 , 7!:2@] NB. time & space
ix =: ([ -. -.) NB. set intersection
1 2 3 4 ix 3 4 5 6 7 NB. eg...
3 4
1998 (ix &.: q:) 2997NB. intersection of factors
999
(ix &.: q:) /1998 2997 3996
999
NB. factoring the greatest common divisor (gcd)
(ts;".)'q:@ (+./) 1998 2997 3996 4995 5994 6993 7992 8991'
+---------------+--------+
|1.45787e_5 3456|3 3 3 37|
+---------------+--------+
NB. using pair-wise intersection of factors
(ts;".) 'q: @(ix &.: q:) /1998 2997 3996 4995 5994 6993 7992 8991'
+--------------+--------+
|6.7494e_5 8640|3 3 3 37|
+--------------+--------+
NB. if need to remove repetitions
(ts;".)'~.@ q:@ (+./) 1998 2997 3996 4995 5994 6993 7992 8991'
+---------------+----+
|1.51187e_5 3712|3 37|
+---------------+----+
(ts;".) '~.@ q: @(ix &.: q:) /1998 2997 3996 4995 5994 6993 7992 8991'
+---------------+----+
|7.07337e_5 7744|3 37|
+---------------+----+
It appears better to remember +.
Cheers,
Mike
On 23/10/2018 08:00, 'Bo Jacoby' via Programming wrote:
q:+./1998 2997 3996 4995 5994 6993 7992 8991
3 3 3 37
Den tirsdag den 23. oktober 2018 08.45.10 CEST skrev 'Rob Hodgkinson' via
Programming <[email protected]>:
This should work Skip, although I changed your call to q: to work on scalars and work with a nested list …
q: each 1998 2997 3996 4995 5994 6993 7992 8991
┌──────────┬──────────┬────────────┬──────────┬────────────┬──────────┬──────────────┬────────────┐
│2 3 3 3 37│3 3 3 3 37│2 2 3 3 3 37│3 3 3 5 37│2 3 3 3 3 37│3 3 3 7 37│2 2 2 3
3 3 37│3 3 3 3 3 37│
└──────────┴──────────┴────────────┴──────────┴────────────┴──────────┴──────────────┴────────────┘
~. each q: each 1998 2997 3996 4995 5994 6993 7992 8991
┌──────┬────┬──────┬──────┬──────┬──────┬──────┬────┐
│2 3 37│3 37│2 3 37│3 5 37│2 3 37│3 7 37│2 3 37│3 37│
└──────┴────┴──────┴──────┴──────┴──────┴──────┴────┘
in=:(([ e. ])#[)
2 3 37 in 3 37
3 37
in each/ ~. each q: each 1998 2997 3996 4995 5994 6993 7992 8991
┌────┐
│3 37│
└────┘
…/Rob
On 23 Oct 2018, at 5:31 pm, Skip Cave <[email protected]> wrote:
Given the integers: 1998 2997 3996 4995 5994 6993 7992 8991
Find the common prime factors in all of these integers.
Obviously we can find the prime factors of each of the integers:
q: 1998 2997 3996 4995 5994 6993 7992 8991
2 3 3 3 37 0 0
3 3 3 3 37 0 0
2 2 3 3 3 37 0
3 3 3 5 37 0 0
2 3 3 3 3 37 0
3 3 3 7 37 0 0
2 2 2 3 3 3 37
3 3 3 3 3 37 0
What J expression will find the common factors in all 6 of these integers?
(the result of the expression should be that there are two common factors -
3 & 37)
Skip
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
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