-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path5-sqrt_recursion.c
43 lines (35 loc) · 926 Bytes
/
5-sqrt_recursion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "main.h"
int find_sqrt(int num, int root);
int _sqrt_recursion(int n);
/**
* find_sqrt - Finds the natural square root of an inputted number.
* @num: The number to find the square root of.
* @root: The root to be tested.
*
* Return: If the number has a natural square root - the square root.
* If the number does not have a natural square root - -1.
*/
int find_sqrt(int num, int root)
{
if ((root * root) == num)
return (root);
if (root == num / 2)
return (-1);
return (find_sqrt(num, root + 1));
}
/**
* _sqrt_recursion - Returns the natural square root of a number.
* @n: The number to return the square root of.
*
* Return: If n has a natural square root - the natural square root of n.
* If n does not have a natural square root - -1.
*/
int _sqrt_recursion(int n)
{
int root = 0;
if (n < 0)
return (-1);
if (n == 1)
return (1);
return (find_sqrt(n, root));
}