-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ3.py
More file actions
47 lines (39 loc) · 1.12 KB
/
Q3.py
File metadata and controls
47 lines (39 loc) · 1.12 KB
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
44
45
46
47
"""
111901030
Mayank Singla
Coding Assignment 7 - Q3
"""
# %%
# Below lines are to ignore the pylint warning in VSCode
# pylint: disable=abstract-method
# pylint: disable=pointless-string-statement
# pylint: disable=invalid-name
def computeNthRoot(n, a, ep):
"""
Computes the nth root of `a` with an error tolerance of ep
"""
low, high = 0, a # lower and higher bound
def f(x):
"""
Function to be evaluated
"""
return x**n - a
while abs(low - high) > ep:
# Using the Bisection method to find the root
mid = (low + high) / 2 # midpoint
if f(mid) == 0:
# if the midpoint is the root
return mid
elif f(mid) * f(low) >= 0:
# if the midpoint is in the same direction as the lower bound
low = mid
else:
# if the midpoint is in the same direction as the higher bound
high = mid
return (low + high) / 2
if __name__ == "__main__":
# Testing the function
m = 19
num = 6**m
eps = 0.00001
print(f"The {m}th root of {num} is {computeNthRoot(m, num, eps)}")