How to test whether the square root of a number will be rational?
After reading comments and the answers to another question I have since asked, I realised that the problem came from a floating point inaccuracy which meant that some values (eg 0.01) would fail the logical test at the end of the program. I have amended it to use NSDecimalNumber
variables instead.
double num, originalnum, multiplier;
int a;
NSLog(@"Enter a number");
scanf("%lf", &num);
//keep a copy of the original number
originalnum = num;
//increases the number until it is an integer, and stores the amount of times it does it in a
for (a=1; fmod(num, 1) != 0 ; a++) {
num *= 10;
}
a--;
//when square-rooted the decimal points have to be added back in
multiplier = pow(10, (a/2));
if (fmod(originalnum, 1) != 0) {
multiplier = 10;
}
NSDecimalNumber *temp = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:sqrt(num)/multiplier] decimalValue]];
NSDecimalNumber *result = [temp decimalNumberByMultiplyingBy:temp];
NSDecimalNumber *originum = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:originalnum] decimalValue]];
if ((fmod(sqrt(num), 1) == 0) && ([result isEqualToNumber:originum])) {
NSLog(@"The square root of %g is %@", originalnum, temp);
}
else {
NSLog(@"The square root of this number is irrational");
}
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.