manish icon

Integral Roots of An Equation

manish | PRO | 01/21/18 07:53:17 AM UTC | 0 ⭐ | 404 👁️ | Never ⏰ | []
Python |

540 B

|

None

|

0 👍

/

0 👎

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

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎