-
Notifications
You must be signed in to change notification settings - Fork 1
/
formula.py
61 lines (43 loc) · 1.25 KB
/
formula.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Formula m√x
import matplotlib.pyplot as plt
import numpy as np
import math
from tqdm import tqdm
from decimal import *
getcontext().prec = 999
m = int(input("Enter m: "))
x = int(input("Enter x: "))
repeat = int(input("Enter repeat: "))
def NormalRoot():
return (Decimal(x) ** (Decimal(1) / Decimal(m)))
xpoints = []
ypoints = []
difXPoints = []
difYPoints = []
NormYPoints = NormalRoot()
def HyunseungRootFormula():
sum = 0
for i in tqdm(range(repeat)):
power = pow(Decimal(x).ln() / m ,i)
sum += Decimal(power) / Decimal(math.factorial(i))
xpoints.append(i)
ypoints.append(sum)
if (i > repeat - 10):
difXPoints.append(i)
difYPoints.append(abs(Decimal(NormYPoints) - sum))
return sum
def graph():
plt.subplot(1, 2, 1)
plt.plot(np.array(xpoints), np.array(ypoints), color="r", label="Hyunseung Root Formula")
plt.axhline(y=NormalRoot(), color='b', linestyle='dotted')
plt.subplot(1, 2, 2)
plt.plot(np.array(difXPoints), np.array(difYPoints), color="r", label="Hyunseung Root Formula Difference")
plt.show()
def main():
norm = NormalRoot()
hyun = HyunseungRootFormula()
print("Normal Root: ", norm)
print("Hyunseung Root: ", hyun)
print("Error: " + str(abs(Decimal(norm) - hyun)))
graph()
main()