Code help: n^2 > 12000

Jul 19, 2011 at 1:44am
I need help writing a loop function. I need to use a while loop to find the smallest integer (n) so that n^2 > 12,000.

expl.

109 x 109 = 11881

110 x 110 = 12100

Therefore, 110 is the smallest value where n2 > 12000. But I need to use a loop to find the solution and I'm just not quite sure how to code this. Could someone please help me? Thanks in advance
Jul 19, 2011 at 2:51am
try:

1
2
int n = 0;
while (n^2  <= 12000) n++;


Of course this is CPU intensive, a better option might be

int n = int(sqrt(12000) + 1);

but then there's no loop...
Last edited on Jul 19, 2011 at 2:56am
Jul 19, 2011 at 2:56am
Hi costaaa, may I know your n^2 is referring to n to the power of 2 ?
Jul 19, 2011 at 3:08am
Just a note that the caret, '^', does not do exponentiation but rather bitwise exclusive or. If you want to do exponentiation, use the pow function in the math library. Or, in the case of n to the power of 2, simply n*n.
Jul 19, 2011 at 3:08am
Yeah, and ausairman's ^ is referring to the xor operator, which is what ^ is in c++.
Jul 19, 2011 at 12:57pm
Oh yes sorry guys, I do mean n to the power of 2 or n*n
Topic archived. No new replies allowed.