forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnthRoot.c
More file actions
42 lines (41 loc) · 642 Bytes
/
nthRoot.c
File metadata and controls
42 lines (41 loc) · 642 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
// question : calculate a^(1/n)
// complexity : O(nlog(n))
long double a,n;
scanf("%Lf%Lf",&a,&n);
long double low=1,high=a;
long double precision = 0.00000001;
if(a>=0 && a<=1)
{
low=a;
high=1;
}
if(a==0)
{
// extra case
printf("0.000000\n");
return 0;
}
while(fabs(high-low)>=precision)
{
long double guess=(low+high)/2;
long double cal=1;
for(int i=1;i<=n;i++)
cal=cal*guess;
// cal = guess^n
if(fabs(cal-a)<=precision)
{
printf("%6Lf\n",guess);
break;
}
else if(cal>a)
high=guess;
else
low=guess;
}
return 0;
}