(posting on-list this time)

On Thu, 11 Jul 2024 at 15:18, Popov, Dmitry Yu via Python-list
<python-list@python.org> wrote:
>
> Dear Sirs.
>
> Does NumPy provide a simple mechanism to identify relatively prime integers, 
> i.e. integers which don't have a common factor other than +1 or -1? For 
> example, in case of this array:
> [[1,5,8],
>   [2,4,8],
>   [3,3,9]]
> I can imagine a function which would return array of common factors along 
> axis 0: [1,2,3]. Those triples of numbers along axis 1 with the factor of1 or 
> -1 would be relatively prime integers.

It sounds like you want the gcd (greatest common divisor) of each row.
The math module can do this:

In [1]: a = [[1,5,8],
   ...:   [2,4,8],
   ...:   [3,3,9]]

In [2]: import math

In [3]: [math.gcd(*row) for row in a]
Out[3]: [1, 2, 3]

NumPy can also do it apparently:

In [10]: np.gcd.reduce(np.transpose(a))
Out[10]: array([1, 2, 3])

https://en.wikipedia.org/wiki/Greatest_common_divisor

--
Oscar
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to