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.
Sqrt[myNumber] \[Element] Rationalsreturns 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 Mar 20 '12 at 0:56Headis 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 useSimplifywithElementto ask Mathematica to spend a bit more time to try to figure out the answer. – Szabolcs Mar 20 '12 at 6:26Headbecause 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 withElement, many thanks :) – dadexix86 Mar 20 '12 at 9:22