How can you test whether the square root of a number will be rational or not?
Is this even possible?
I need this because I need to work out whether to display a number as a surd or not in a maths app I'm developing at the moment.
Identifying Irrational NumbersIf a square root is not a perfect square, then it is considered an irrational number. These numbers cannot be written as a fraction because the decimal does not end (non-terminating) and does not repeat a pattern (non-repeating).
A rational number is the one which can be represented in the form of P/Q where P and Q are integers and Q ≠ 0. But an irrational number cannot be written in the form of simple fractions. ⅔ is an example of a rational number whereas √2 is an irrational number.
√2, √3, √5, and so on are some examples of irrational numbers as they cannot be expressed in form of p ⁄ q.
For integer inputs, only the square roots of the square numbers are rationals. So your problem boils down to find if your number is a square number. Compare the question: What's a good algorithm to determine if an input is a perfect square?.
If you have rational numbers as inputs (that is, a number given as the ratio between two integer numbers), check that both divisor and dividend are perfect squares.
For floating-point values, there is probably no solution because you can't check if a number is rational with the truncated decimal representation.
From wikipedia: The square root of x is rational if and only if x is a rational number that can be represented as a ratio of two perfect squares.
So you need to find a rational approxmiation for your input number. So far the only algorithm I've nailed down that does this task is written in Saturn Assembler for the HP48 series of calculators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With