Advertisement
manish

Integral Roots of An Equation

Jan 21st, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. def equation(x):
  2.     s, found, roots = 0, 0, ''
  3.     for i in x:
  4.         s += abs(i)
  5.  
  6.     for i in range(-1 * s, s + 1):
  7.         val = 0
  8.         for j in range(len(x)):
  9.             val += x[j] * (i ** (len(x) - j - 1))
  10.         if val == 0:
  11.             roots += ', ' + str(i)
  12.             found += 1
  13.     print('The equation has %s distinct integer roots: %s' % (found, roots[1:]))
  14.  
  15.  
  16. equation([1, 1, -39, -121, -10, 168])  # Represents x^5 + x^4 - 39x^3 - 121x^2 - 10x + 168 = 0
  17. equation([1, 0, -13, 0, 36])  # Represents x^4 - 13x^2 + 36 = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement