Function Repository Resource:

CommonDivisors

Source Notebook

Find the common divisors of two or more integers

Contributed by: Wolfram|Alpha Math Team

ResourceFunction["CommonDivisors"][{n1,n2,}]

gives the common divisors of the integers ni.

ResourceFunction["CommonDivisors"][n]

gives the integer divisors of an integer n.

Details and Options

ResourceFunction["CommonDivisors"] works only with integers and Gaussian integers.
The input to ResourceFunction["CommonDivisors"] must be exact, not approximate, numbers.

Examples

Basic Examples (2) 

Find the common divisors of a list of integers:

In[1]:=
ResourceFunction["CommonDivisors"][{3, 6, 9, 18}]
Out[1]=

Find the divisors of a single integer:

In[2]:=
ResourceFunction["CommonDivisors"][120]
Out[2]=

Scope (2) 

Find the common divisors of a list of powers:

In[3]:=
ResourceFunction["CommonDivisors"][2^Range[6, 10]]
Out[3]=

CommonDivisors can handle Gaussian integers:

In[4]:=
ResourceFunction["CommonDivisors"][{10, 8 + 4 I}]
Out[4]=

Applications (1) 

Make a table showing that the first five primes share no proper common divisors other than 1:

In[5]:=
n = 5;
primes = Prime[Range[n]]
Out[5]=
In[6]:=
data = Outer[ResourceFunction["CommonDivisors"][{#1, #2}] &, primes, primes];
headerLines = {Join[{"CommonDiviors[n, m]"}, Table[SpanFromLeft, n - 1]], Prepend[primes, {"n=/m="}]};
Grid[headerLines~Join~Thread[Prepend[data, primes]], Frame -> All, Background -> {{LightGray, None}, {LightPurple, LightGray, {None}}}]
Out[6]=

Possible Issues (1) 

The input to CommonDivisors must consist of exact, not approximate, numbers:

In[7]:=
ResourceFunction["CommonDivisors"][{2.0, 4}]
Out[7]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 6.0.0 – 23 March 2023
  • 5.0.0 – 16 April 2020
  • 4.0.0 – 16 April 2020
  • 3.0.0 – 24 January 2020
  • 2.0.0 – 06 September 2019
  • 1.0.0 – 23 July 2019

Related Resources

Author Notes

To view the full source code for CommonDivisors, evaluate the following:

In[1]:=
SystemOpen[
 FileNameJoin[
  ReplacePart[
   FileNameSplit[FindFile["ResourceFunctionHelpers`"]], -1 -> "CommonMultiplesAndDivisors.wl"]]]

License Information