How to know if a number is the square of a rational?

14

1

I'm pretty new with Mathematica and I was looking for a way to know whether a number is a square of a rational. I thought of

Head[Sqrt[myNumber]] == Rational

where myNumber came from replacing some variables with rational and integer numbers in a polynomial equation.

Is there a better way to do it for large numbers, perhaps without computing the square root?

dadexix86

Posted 2012-03-20T00:50:29.410

Reputation: 241

7For starters, Sqrt[myNumber] ∈ Rationals returns a correct result for integers and your solution doesn't, so I guess it's an improvement. However, I don't know what results you want for machine precision numbers... Sidenote: Use triple === when you want the result to be False when it is not True. == remains unevaluated for symbolic arguments – Rojo – 2012-03-20T00:56:15.367

1I'd like to complement Rojo's comment a little: Head is a programming construct which test "data types". Element[number, Rationals] is a mathematical construct which tries to test if a number is mathematically a rational number. It may be necessary to use Simplify with Element to ask Mathematica to spend a bit more time to try to figure out the answer. – Szabolcs – 2012-03-20T06:26:15.270

First, thanks to both. I was using Head because checking the internal type seems to me the fastest way to do it. It should not work at all with machine precision number, I don't really need to use them. I'll do it with Element, many thanks :) – dadexix86 – 2012-03-20T09:22:02.537

Answers

6

The only trick I can see is a trivial one: if $x$ is the square of a rational, it is also a rational. That's because of the $x = (a/b)^2 = a^2/b^2$.

So, I'd write a function testing the rationality, which returns either True, False, or Null (if rationality cannot be established):

isRational[x_] := If[Simplify[x ∈ Rationals], True, False, Null]

which works like this:

In[30]:= isRational /@ {1/3, π, EulerGamma}
Out[30]= {True, False, Null}

And then simply use it by first checking if the number itself is known to be rational:

isSqrRational[x_] := If[isRational[x], isRational[Sqrt[x]], False, isRational[Sqrt[x]]]

which gives:

In[33]:= isSqrRational /@ {1/3, 1/4, π, EulerGamma}
Out[33]= {False, True, False, Null}

F'x

Posted 2012-03-20T00:50:29.410

Reputation: 10 517

3

Here's a way to test if a number is a square of a rational, without taking square roots:

SetAttributes[squareQ, Listable];
squareQ[x_] := MatchQ[Head[x], Integer | Rational] && 
                      And @@ OddQ[DivisorSigma[0, Through[{Numerator, Denominator}[x]]]]

See e.g. this for a proof.

J. M.'s ennui

Posted 2012-03-20T00:50:29.410

Reputation: 115 520

I can only assume that avoiding square roots is to reduce computational complexity. How well does DivisorSigma[0, n] scale with n? – Corey Kelly – 2013-06-07T11:10:38.157

2

How do you know if a number $a$ is the square of a rational?

Lemma: The rationals which are squares of rational numbers are themselves either squares of whole numbers or ratios of such squares.

Algorithm:

So if you're given a rational number, say $a=\frac{n}{d}$ where $n$ and $d$ have no common divisors, then $a$ is the square of a rational if and only if $n$ and $d$ are also squares of whole numbers.

Suppose $a= 0.6428571428571428571$. First you convert it into fractions without common factors:

Rationalize[0.6428571428571428571]
9/14

n=Numerator[Rationalize[0.6428571428571428571]]
d=Denominator[Rationalize[0.6428571428571428571]]

Then you gotta check if all of the exponents of the prime factors of $n$ and $d$ are even.

Now FactorInteger does the job here. It lists primes and its exponents:

FactorInteger[n]
{{3, 2}}
FactorInteger[d]
{{2, 1}, {7, 1}}

Now if the 2nd entries of both of these matrices are even then you're golden. For this you use EvenQ

EvenQ[#[[2]] & /@ FactorInteger[n]]
{True}
EvenQ[#[[2]] & /@ FactorInteger[d]]
{False, False}

If any False shows up then your answer is No.

Proof of Lemma:

Well, if $a$ is itself irrational then it cannot be the square of a rational by definition.

So now we need to know when a rational number $a$ is the square of rationals.

So lets suppose $a =\frac{n}{d}$ , a rational number where $n$ and $d$ are whole numbers that have no common divisors.

Now if $a=\frac{n}{d} = \frac{g^2}{h^2}$, where $g$ and $h$ are whole numbers with no common divisors.

then

$n*h^2 = d*g^2$ ---(1)

Let $\prod_k p^{a_k}_k$ be the prime factorization of $n$ then each $a_k$ must be even, since $p_k$ divides $d*g^2$ and since $p_k$ does not divide $d$, it must divide $g$. This implies that $p_k$ shows up with an even exponent on the rhs of (1). Also, since $g$ and $h$ don't have any common factors, $p_k$ shows up on the lhs only under the factorization of $n$. Therefore $a_k$ is even. Since $p_k$ was arbitrarily chosen, the exponents of all prime divisors of $n$ are even. Therefore $n$ is a perfect square. By symmetry, the same argument demonstrates that $d$ is a perfect square as well.

QED.

Amatya

Posted 2012-03-20T00:50:29.410

Reputation: 6 498