-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton_separate.py
74 lines (52 loc) · 1.58 KB
/
newton_separate.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
62
63
64
65
66
67
68
69
70
71
72
73
74
from fractions import Fraction
def gcd(x,y):
if x%y == 0:
return y
return gcd(y,x%y)
def check(deg,terms,x,y):
check_saperate = 0
reply = ""
sum = 0
root = Fraction(y,x)*(-1)
for i4 in range (0,deg+1):
sum += terms[i4]*root**i4
if sum == 0:
if x==1:
reply += "(x+{})".format(y)
else:
reply += "({}x+{})".format(x,y)
check_saperate += 1
sum = 0
root*=(-1)
for i5 in range (0,deg+1):
sum += terms[i5]*root**i5
if sum == 0:
if x==1:
reply += "(x-{})".format(y)
else:
reply += "({}x-{})".format(x,y)
check_saperate += 1
return check_saperate, reply
def run_main(input_terms):
#input_terms = input('Please input the terms of each degree, and saperate by commmas => ')
terms_temp = input_terms.split(',')
deg = len(terms_temp)-1
can_it_separate = 0
terms = []
reply = ""
for i1 in range (0,deg+1):
terms.append(terms_temp[deg-i1])
terms[i1] = int(terms[i1])
terms_constant = abs(terms[0])
terms_highest = abs(terms[deg])
for i2 in range (1,terms_highest+1):
if terms_highest%i2 == 0:
for i3 in range (1,terms_constant+1):
if terms_constant%i3 == 0:
if gcd(i2,i3) == 1:
temp, reply_temp = check(deg,terms,i2,i3)
can_it_separate += temp
reply += reply_temp
if can_it_separate == 0:
reply += 'Sorry, there is no result.'
return reply