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
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!
Clarify that number is a positive integer. You are welcome to make code that would do any number but it is not necessary.
Clarify that comparison operators are allowed.
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.
Clarify that we need at least 5 digits. 10 digits caused code to run for a long time.