---
title: "SquareFreeQ"
language: "en"
type: "Symbol"
summary: "SquareFreeQ[expr] gives True if expr is a square-free polynomial or number, and False otherwise. SquareFreeQ[expr, vars] gives True if expr is square free with respect to the variables vars."
keywords: 
- square-free integers
- square-free polynomials
- isqrfree
- issqrfree
canonical_url: "https://reference.wolfram.com/language/ref/SquareFreeQ.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Number Theoretic Functions"
    link: "https://reference.wolfram.com/language/guide/NumberTheoreticFunctions.en.md"
  - 
    title: "Polynomial Factoring & Decomposition"
    link: "https://reference.wolfram.com/language/guide/PolynomialFactoring.en.md"
  - 
    title: "Prime Numbers"
    link: "https://reference.wolfram.com/language/guide/PrimeNumbers.en.md"
  - 
    title: "Number Recognition"
    link: "https://reference.wolfram.com/language/guide/NumberRecognition.en.md"
related_functions: 
  - 
    title: "FactorInteger"
    link: "https://reference.wolfram.com/language/ref/FactorInteger.en.md"
  - 
    title: "Factor"
    link: "https://reference.wolfram.com/language/ref/Factor.en.md"
  - 
    title: "FactorSquareFree"
    link: "https://reference.wolfram.com/language/ref/FactorSquareFree.en.md"
  - 
    title: "FactorSquareFreeList"
    link: "https://reference.wolfram.com/language/ref/FactorSquareFreeList.en.md"
  - 
    title: "PolynomialGCD"
    link: "https://reference.wolfram.com/language/ref/PolynomialGCD.en.md"
  - 
    title: "IrreduciblePolynomialQ"
    link: "https://reference.wolfram.com/language/ref/IrreduciblePolynomialQ.en.md"
  - 
    title: "Discriminant"
    link: "https://reference.wolfram.com/language/ref/Discriminant.en.md"
  - 
    title: "PrimeQ"
    link: "https://reference.wolfram.com/language/ref/PrimeQ.en.md"
  - 
    title: "PrimePowerQ"
    link: "https://reference.wolfram.com/language/ref/PrimePowerQ.en.md"
  - 
    title: "MoebiusMu"
    link: "https://reference.wolfram.com/language/ref/MoebiusMu.en.md"
  - 
    title: "PrimeNu"
    link: "https://reference.wolfram.com/language/ref/PrimeNu.en.md"
  - 
    title: "PrimeOmega"
    link: "https://reference.wolfram.com/language/ref/PrimeOmega.en.md"
  - 
    title: "DuplicateFreeQ"
    link: "https://reference.wolfram.com/language/ref/DuplicateFreeQ.en.md"
  - 
    title: "Zeta"
    link: "https://reference.wolfram.com/language/ref/Zeta.en.md"
related_tutorials: 
  - 
    title: "Integer and Number Theoretic Functions"
    link: "https://reference.wolfram.com/language/tutorial/MathematicalFunctions.en.md#20170"
  - 
    title: "Algebraic Operations on Polynomials"
    link: "https://reference.wolfram.com/language/tutorial/AlgebraicManipulation.en.md#13694"
---
# SquareFreeQ

SquareFreeQ[expr] gives True if expr is a square-free polynomial or number, and False otherwise.

SquareFreeQ[expr, vars] gives True if expr is square free with respect to the variables vars.

## Details and Options

* ``SquareFreeQ`` is typically used to test whether a number or a polynomial is square free.

* An integer ``n`` is square free if it is divisible by no perfect square other than 1.

[image]

* ``SquareFreeQ[expr]`` returns ``False`` unless ``expr`` is manifestly square free.

* With the setting ``GaussianIntegers -> True``, ``SquareFreeQ`` tests whether ``expr`` is Gaussian square free.

* For integers ``m`` and ``n``, ``SquareFreeQ[m + I n]`` automatically works over Gaussian integers.

* The following options can be given:

|                   |           |                                     |
| ----------------- | --------- | ----------------------------------- |
| GaussianIntegers  | Automatic | whether to allow Gaussian integers  |
| Modulus           | 0         | modulus for polynomial coefficients |

---

## Examples (28)

### Basic Examples (2)

Test whether a number is square free:

```wl
In[1]:= SquareFreeQ[10]

Out[1]= True
```

---

The number 4 is not square free:

```wl
In[1]:= SquareFreeQ[4]

Out[1]= False
```

### Scope (5)

``SquareFreeQ`` works over integers:

```wl
In[1]:= SquareFreeQ[20]

Out[1]= False
```

Gaussian integers:

```wl
In[2]:= SquareFreeQ[3 + 2I]

Out[2]= True

In[3]:= SquareFreeQ[2, GaussianIntegers -> True]

Out[3]= False
```

Rational numbers:

```wl
In[4]:= SquareFreeQ[2 / 3]

Out[4]= True
```

---

Univariate polynomials:

```wl
In[1]:= SquareFreeQ[6 + 6x + x ^ 2]

Out[1]= True
```

Multivariate polynomials:

```wl
In[2]:= SquareFreeQ[x ^ 3 - x ^ 2y]

Out[2]= False
```

---

Specify the variable in a polynomial:

```wl
In[1]:= SquareFreeQ[x y ^ 2, x]

Out[1]= True

In[2]:= SquareFreeQ[x y ^ 2, y]

Out[2]= False
```

---

Polynomials over a finite field:

```wl
In[1]:= SquareFreeQ[x ^ 2 + 1, Modulus -> 2]

Out[1]= False
```

---

Test for large integers:

```wl
In[1]:= SquareFreeQ[10 ^ 70 + 3]

Out[1]= True
```

### Options (2)

#### GaussianIntegers (1)

Test whether 2 is square free over integers:

```wl
In[1]:= SquareFreeQ[2]

Out[1]= True
```

Gaussian integers:

```wl
In[2]:= SquareFreeQ[2, GaussianIntegers -> True]

Out[2]= False
```

#### Modulus (1)

Test whether $x^2-3$ is square free over integers:

```wl
In[1]:= SquareFreeQ[x ^ 2 - 3]

Out[1]= True
```

Integers modulo 3:

```wl
In[2]:= SquareFreeQ[x ^ 2 - 3, Modulus -> 3]

Out[2]= False
```

### Applications (8)

#### Basic Applications (3)

Highlight square-free numbers:

```wl
In[1]:= Multicolumn[If[SquareFreeQ[#], Style[#, Red, Bold], #]& /@ Range[100], 10, ...]

Out[1]=
|        |        |        |        |        |        |        |        |        |        |
| ------ | ------ | ------ | ------ | ------ | ------ | ------ | ------ | ------ | ------ |
| **1**  | **2**  | **3**  | 4      | **5**  | **6**  | **7**  | ...  **74** | 75     | 76     | **77** | **78** | **79** | 80     |
| 81     | **82** | **83** | 84     | **85** | **86** | **87** | 88     | **89** | 90     |
| **91** | 92     | **93** | **94** | **95** | 96     | **97** | 98     | 99     | 100    |
```

---

Generate random square-free integers:

```wl
In[1]:= randomSquareFree[m_] := Table[Times@@Union[RandomPrime[{2, 1000}, RandomInteger[{1, 5}]]], {m}];

In[2]:= randomSquareFree[5]

Out[2]= {451964058907, 5006839741, 75723749, 129391589, 179}

In[3]:= AllTrue[%, SquareFreeQ]

Out[3]= True
```

---

Square-free Gaussian integers:

```wl
In[1]:= ArrayPlot[Table[Boole[SquareFreeQ[x + I y, GaussianIntegers -> True]], {x, 300}, {y, 300}]]

Out[1]= [image]
```

#### Number Theory (5)

The central binomial coefficients ``Binomial[2n, n]`` are not square free for $n>4$ :

```wl
In[1]:= And @@ Table[Not[SquareFreeQ[Binomial[2n, n]]], {n, 5, 2 ^ 10}]

Out[1]= True
```

---

Find the fraction of the first $10^5$ numbers that are square free:

```wl
In[1]:= Length@Select[Range[10 ^ 5], SquareFreeQ] / 10 ^ 5//N

Out[1]= 0.60794
```

The result is close to $1/\zeta (2)$ :

```wl
In[2]:= 1 / Zeta[2]//N

Out[2]= 0.607927
```

---

The polynomial ``p[x] / PolynomialGCD[p[x], p'[x]]`` is always square free:

```wl
In[1]:= p = (x + 1) ^ 3(x + 2) ^ 2(x + 3) ^ 2;

In[2]:= q = p / PolynomialGCD[p, D[p, x]]//FullSimplify

Out[2]= (1 + x) (2 + x) (3 + x)

In[3]:= SquareFreeQ[q]

Out[3]= True
```

---

The distribution of square-free numbers over integers:

```wl
In[1]:= data  = Select[Range[100], SquareFreeQ];

In[2]:= \[ScriptCapitalD] = EmpiricalDistribution[data];
```

Plot the distribution:

```wl
In[3]:= DiscretePlot[Evaluate[CDF[\[ScriptCapitalD], x]], {x, 1, 100}, ExtentSize -> Right]

Out[3]= [image]
```

---

The distribution of square-free numbers over the Gaussian integers:

```wl
In[1]:= data = {Re[#], Im[#]}& /@ Select[Flatten[Table[a + b I, {a, 500}, {b, 500}]], SquareFreeQ];

In[2]:= \[ScriptCapitalD] = EmpiricalDistribution[data];
```

Plot the distribution:

```wl
In[3]:= DiscretePlot3D[Evaluate[CDF[\[ScriptCapitalD], {x, y}]], {x, 1, 5}, {y, 1, 5}, ExtentSize -> Right]

Out[3]= [image]
```

### Properties & Relations (8)

A number that is divisible by a square is not square free:

```wl
In[1]:= Divisible[12, 4]

Out[1]= True

In[2]:= SquareFreeQ[12]

Out[2]= False
```

---

In the prime factorization of a square-free number, the exponents of primes are all 1:

```wl
In[1]:= SquareFreeQ[210]

Out[1]= True

In[2]:= FactorInteger[210]

Out[2]= {{2, 1}, {3, 1}, {5, 1}, {7, 1}}
```

---

``PrimeNu`` is equal to ``PrimeOmega`` for square-free numbers:

```wl
In[1]:= SquareFreeQ[14]

Out[1]= True

In[2]:= PrimeNu[14] == PrimeOmega[14]

Out[2]= True
```

---

``MoebiusMu`` is zero for non-square-free integers:

```wl
In[1]:= SquareFreeQ[12]

Out[1]= False

In[2]:= MoebiusMu[12]

Out[2]= 0
```

---

Numbers that are prime powers and square free are prime numbers:

```wl
In[1]:= PrimePowerQ[17] && SquareFreeQ[17]

Out[1]= True

In[2]:= PrimeQ[17]

Out[2]= True
```

---

The discriminant of a quadratic non-square-free polynomial is 0:

```wl
In[1]:= SquareFreeQ[x ^ 2 - 2x + 1, x]

Out[1]= False

In[2]:= Discriminant[x ^ 2 - 2x + 1, x]

Out[2]= 0
```

---

Square factors can be found using ``FactorSquareFreeList`` :

```wl
In[1]:= SquareFreeQ[Expand[(x + 1) ^ 3(x + 2) ^ 2(x + 3) ^ 2]]

Out[1]= False

In[2]:= FactorSquareFreeList[Expand[(x + 1) ^ 3(x + 2) ^ 2(x + 3) ^ 2]]

Out[2]= {{1, 1}, {1 + x, 3}, {6 + 5 x + x^2, 2}}
```

---

Simplify symbolic expressions:

```wl
In[1]:= Simplify[SquareFreeQ[a ^ 2], a∈Integers]

Out[1]= False
```

### Neat Examples (3)

Plot the prime numbers that are the sum of three squares:

```wl
In[1]:= ArrayMesh[Boole[Table[SquareFreeQ[a ^ 2 + b ^ 2 + c ^ 2], {a, 10}, {b, 10}, {c, 10}]]]

Out[1]= [image]
```

---

Square-free Gaussian integers:

```wl
In[1]:= ArrayPlot[Table[Boole[SquareFreeQ[x + I y]], {x, 50}, {y, 50}]]

Out[1]= [image]
```

---

Plot the Ulam spiral of square-free numbers:

```wl
In[1]:=
ulam[n_] := Partition[Permute[Range[n ^ 2], Accumulate[Take[Flatten[{{n ^ 2 + 1} / 2, Table
	[(-1) ^ j i, {j, n}, {i, {-1, n}}, {j}]}], n ^ 2]]], n];

In[2]:= ArrayPlot[ulam[101]MapAt[Boole[SquareFreeQ[#]]&, ulam[101], {All, All}], ColorFunction -> "Rainbow", ColorRules -> {0 -> White}]

Out[2]= [image]
```

## See Also

* [`FactorInteger`](https://reference.wolfram.com/language/ref/FactorInteger.en.md)
* [`Factor`](https://reference.wolfram.com/language/ref/Factor.en.md)
* [`FactorSquareFree`](https://reference.wolfram.com/language/ref/FactorSquareFree.en.md)
* [`FactorSquareFreeList`](https://reference.wolfram.com/language/ref/FactorSquareFreeList.en.md)
* [`PolynomialGCD`](https://reference.wolfram.com/language/ref/PolynomialGCD.en.md)
* [`IrreduciblePolynomialQ`](https://reference.wolfram.com/language/ref/IrreduciblePolynomialQ.en.md)
* [`Discriminant`](https://reference.wolfram.com/language/ref/Discriminant.en.md)
* [`PrimeQ`](https://reference.wolfram.com/language/ref/PrimeQ.en.md)
* [`PrimePowerQ`](https://reference.wolfram.com/language/ref/PrimePowerQ.en.md)
* [`MoebiusMu`](https://reference.wolfram.com/language/ref/MoebiusMu.en.md)
* [`PrimeNu`](https://reference.wolfram.com/language/ref/PrimeNu.en.md)
* [`PrimeOmega`](https://reference.wolfram.com/language/ref/PrimeOmega.en.md)
* [`DuplicateFreeQ`](https://reference.wolfram.com/language/ref/DuplicateFreeQ.en.md)
* [`Zeta`](https://reference.wolfram.com/language/ref/Zeta.en.md)

## Tech Notes

* [Integer and Number Theoretic Functions](https://reference.wolfram.com/language/tutorial/MathematicalFunctions.en.md#20170)
* [Algebraic Operations on Polynomials](https://reference.wolfram.com/language/tutorial/AlgebraicManipulation.en.md#13694)

## Related Guides

* [Number Theoretic Functions](https://reference.wolfram.com/language/guide/NumberTheoreticFunctions.en.md)
* [Polynomial Factoring & Decomposition](https://reference.wolfram.com/language/guide/PolynomialFactoring.en.md)
* [Prime Numbers](https://reference.wolfram.com/language/guide/PrimeNumbers.en.md)
* [Number Recognition](https://reference.wolfram.com/language/guide/NumberRecognition.en.md)

## History

* [Introduced in 2007 (6.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn60.en.md)