Sorry for the poor title. I couldn’t find a better title. (Edits are welcome :p )
Lets say that there is a number X=8. I know 2*2=4 so it does not contain the number 8.
EDIT: imagine a 2×2 grid … 1,2,3,4 … it does not contain 8
Now, 3*3 = 9 and we have found our winner(n=3)!
My (poor) code for this purpose
long long int i=0; N = 8;
while (i*i <N)
{
i++;
}
l = i; // l is to store he number "3"; say 8 is in a 3x3 grid
This is not a efficient way to calculate when N = 100000000.
What is more efficient way to get i*i that contains X ?
Use the
sqrtfunction from<math.h>. It’ll be a (fairly) efficient algorithm, and should run much faster than your loop. Then round up, and you have your answer.