13
5
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.
Are comparisons such as
<
and>
disallowed operators? – Greg Hewgill – 2014-06-06T02:06:23.423Comparisons are fine, however the only operation that can change the value of a variable (or constant, I suppose) are ++ and --, with the exceptions listed above. – iggyvolz – 2014-06-06T02:07:58.660
Are bitwise operators allowed? And to clarify, is -- allowed or not... as it isn't stated in the question, but in your comment you mentioned it. – Οurous – 2014-06-06T02:12:20.123
1Nope, -- is not allowed, sorry for the confusion! I originally planned to have ++ and -- but I decided to take -- out at the last minute. – iggyvolz – 2014-06-06T02:15:01.417
@iggyvolz ... and bitwise? – Οurous – 2014-06-06T02:20:33.217
5"without using any mathematical operators to change the number" - I think this might need clarification. Do you mean that these operators may not be used at all, or, that they may be used, but only if the result isn't saved to a variable, e.g.
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. – primo – 2014-06-06T02:56:07.1271Why would you ever want to "change" any variable at all? You imperative guys have strange ways of thinking... – ceased to turn counterclockwis – 2014-06-06T10:20:50.230
Are we allowed to do, say,
i=1
? That is, can we initialise a variable that will then be incremented? – Glen O – 2014-06-06T14:24:17.643@leftaroundabout 'static variable' is self-contradictive. – primo – 2014-06-06T16:57:37.940
@Ourous - oh sorry, missed that part. If the function in itself changes the value of any variable, it is not allowed. – iggyvolz – 2014-06-06T19:56:57.933
@primo - I'll reduce it to 10^8, but answers with 10^10 will still be allowed – iggyvolz – 2014-06-06T20:00:04.930
@GlenO - "If you wish, you may initialize a variable by setting it to 0." You can do i=0, but not i=1. If you needed i=1, you could always do i=0;i++; – iggyvolz – 2014-06-06T20:00:58.313
4I am flagging to close this question. To much radical changes to the question. You should actually get this question validated through Sandbox or else you would frustrate people investing effort to answer. – Abhijit – 2014-06-06T20:21:08.797
3Reducing the number of required digits is meaningless without time/memory limits. My code can handle 5 digits, but my machine doesn't have enough RAM. – Dennis – 2014-06-07T02:11:51.227