def equation(x):
s, found, roots = 0, 0, ''
for i in x:
s += abs(i)
for i in range(-1 * s, s + 1):
val = 0
for j in range(len(x)):
val += x[j] * (i ** (len(x) - j - 1))
if val == 0:
roots += ', ' + str(i)
found += 1
print('The equation has %s distinct integer roots: %s' % (found, roots[1:]))
equation([1, 1, -39, -121, -10, 168]) # Represents x^5 + x^4 - 39x^3 - 121x^2 - 10x + 168 = 0
equation([1, 0, -13, 0, 36]) # Represents x^4 - 13x^2 + 36 = 0
Comments
0 B
|👍
/👎