libft
ft_sqrt.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* ft_sqrt.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2019/09/20 21:48:17 by unite #+# #+# */
10 /* Updated: 2020/07/16 02:52:47 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
20 int ft_sqrt(int num)
21 {
22  int factor;
23 
24  if (num < 0 ||
25  (num % 2 == 0 && num % 4 != 0) ||
26  (num % 3 == 0 && num % 9 != 0))
27  return (-1);
28  factor = (num % 2) ? 1 : 0;
29  while (factor < num / 2)
30  {
31  if (factor * factor == num)
32  return (factor);
33  factor += 2;
34  }
35  return (-1);
36 }
ft_sqrt
int ft_sqrt(int num)
Computes an integer square root of a given number, if it exists.
Definition: ft_sqrt.c:20