Last review
I implemented it in consideration of speed. Create your own code that returns the square root of C language --Qiita https://qiita.com/Kchan_01/items/51a9bda94ac62e3ef56c
--Create a function that returns the square root of the number (if it exists). --0 if the square root is an irrational number. --Return within 2 seconds.
int ft_sqrt(int nb)
{
int root;
if(nb <= 0 || nb > (46340 * 46340))
return (0);
root = 0;
// while(nb >= root && root <= 46340){Please point out and rewrite
while(nb >= root * root){
root++;
if(nb == root * root)
return (root);
}
return (0);
}
for(int i = -10; i < 30; i++)
printf("%d : sqrt : %d\n", i, ft_sqrt(i));
printf("%d\n", ft_sqrt(46340 * 46340));
printf("%d\n", ft_sqrt(46340 * 46340 + 1));
printf("%d\n", ft_sqrt(46340 * 46340 - 1));
printf("%d\n", ft_sqrt(INT_MAX));
At the time of calculation, I didn't realize that the upper limit of int * int was int, so I spent about 20 minutes. Even if you assign it to a long int, it is still calculated between ints, so be careful.
Problem understanding 7 minutes Coding 10 minutes Test case 25 minutes Final coding 5 minutes
Recommended Posts