Your task is to calculate the square root of a positive integer without using any mathematical operators to change the number, such as:
- Setting a variable (ex. squareRoot = 5)
- Addition (A+B)
- Subtraction (A-B)
- Multiplication (A*B)
- Division (A/B)
- Square, cube, fourth, etc. roots
- Exponents
Comparison operators (such as <, >, ==, etc) are not considered "mathematical operators" for the purposes of this question and are allowed as long as they do not change the value of a variable.
The only operator that you are able to use is ++. The following exceptions are in place:
- If you wish, you may initialize a variable by setting it to 0.
- If your language does not include the ++ syntax, you may use an equivalent syntax, such as foo+=1 or foo=foo+1
- The square root should be calculated to at least 6 digits beyond the decimal (the hundred-thousands place) and outputted as a whole number of the decimals (ex. if I input 2 it could come out as 14142135624 or 1414213 depending on the rounding). Rounding up or down is not important.
User-defined functions are not allowed. In addition, simulating functions with goto is not allowed as well.
I'm interested to see what everyone submits! Happy coding!
CLARIFICATION
Clarify that number is a positive integer. You are welcome to make code that would do any number but it is not necessary.
CLARIFICATION #2
Clarify that comparison operators are allowed.
CLARIFICATION #3
Addition, subtraction, multiplication, division, and functions to change numbers are not allowed at all, regardless of whether they are saved to a variable or not. I'm sorry that this invalidates a couple existing answers, but I meant to define this group of operators with "change the number" in order to prevent troll answers (ex. I just used the sqrt() function, you only prohibited addition, multiplication, division, and subtraction). Sorry for the confusion.
CLARIFICATION #4
Clarify that we need at least 5 digits. 10 digits caused code to run for a long time.
while r*r<n*10e20:r+=1
- fairly trivial. Also, you might consider reducing the required output to 10^8 or so. First, because 10^10 is larger than 2^31, and second, because it will take a while to increment that high. \$\endgroup\$