I require this because I should work out whether to display a number together a surd or not in a maths application I"m emerging at the moment.

You are watching: Can square roots be rational numbers


*

For essence inputs, only the square roots of the square numbers space rationals. For this reason your difficulty boils under to discover if her number is a square number. To compare the question: What"s a an excellent algorithm to determine if an intake is a perfect square?.

If you have rational numbers as inputs (that is, a number provided as the ratio between two essence numbers), inspect that both divisor and dividend are perfect squares.

For floating-point values, there is probably no solution due to the fact that you can"t inspect if a number is rational with the truncated decimal representation.


*

*

From wikipedia: The square source of x is rational if and only if x is a reasonable number that deserve to be represented as a proportion of 2 perfect squares.

So you need to find a reasonable approxmiation for your input number. So much the only algorithm I"ve nailed under that walk this task is composed in Saturn Assembler because that the HP48 collection of calculators.


*

After analysis comments and also the answer to an additional question ns have because asked, i realised the the problem came native a floating suggest inaccuracy which expected that some worths (eg 0.01) would certainly fail the logical check at the end of the program. I have actually amended that to use NSDecimalNumber variables instead.

See more: Short Summary: How Is Math Used In Baseball Math, Teaching Math Through Major League Baseball

double num, originalnum, multiplier;int a;NSLog(
"Enter a number");scanf("%lf", &num);//keep a copy of the original numberoriginalnum = num;//increases the number until it is an integer, and stores the lot of time it does that in afor (a=1; fmod(num, 1) != 0 ; a++) num *= 10;a--;//when square-rooted the decimal points need to be added back inmultiplier = pow(10, (a/2));if (fmod(originalnum, 1) != 0) multiplier = 10;NSDecimalNumber *temp = decimalValue>>;NSDecimalNumber *result = ;NSDecimalNumber *originum = decimalValue>>;if ((fmod(sqrt(num), 1) == 0) && ()) NSLog(

*

If you"re handling integers, note that a confident integer has a rational square source if and only if it has actually an integer square root, that is, if the is a perfect square. For information on experimentation for that, please view this amazing smashville247.net question.


On https://math.stackexchange.com/ there is the concern What reasonable numbers have actually rational square roots? the yielded solution from Jakube that says that because that "...rational numbers, an answer is to identify if the numerator and denominator are integers elevated to the strength of 2."

Good methods to work out whether organic numbers are perfect squares relies on the natural numbers the function supports (and the computer system programming language gift used) and the memory available etc. Below are a set of beneficial links:

I developed and also tested a equipment in Java that works well enough for me with a set of natural numbers. The gist that this is provided below. This code depends on BigMath and is applied in agdt-java-math albeit in a pair of different classes:

/** *
code null otherwise. */ public static BigRational getSqrtRational(BigRational x) BigInteger<> numden = getNumeratorAndDenominator(x); BigInteger nums = getPerfectSquareRoot(numden<0>); if (nums != null) BigInteger dens = getPerfectSquareRoot(numden<1>); if (dens != null) return BigRational.valueOf(nums).divide(BigRational.valueOf(dens)); return null; /** *
code x} */ public static BigInteger<> getNumeratorAndDenominator(BigRational x) BigInteger<> r = brand-new BigInteger<2>; r<0> = x.getNumeratorBigInteger(); r<1> = x.getDenominatorBigInteger(); if (Math_BigInteger.isDivisibleBy(r<0>, r<1>)) r<0> = r<0>.divide(r<1>); r<1> = BigInteger.ONE; return r; /** *