#!/usr/bin/env python
# -*- coding: utf-8 -*-
#   !!!!!!!ENGLISH VERSION!!!!!!!
#   Ruffini automatic divisors! You need to do a lot of operations to find an exact divisor on ruffini, this tool does it for you!
#
#   Programmer Social Networks:
#   Twitter: https://twitter.com/ecomaikgolf   Github: https://github.com/ecomaikgolf   E-mail: iecomaikgolf@gmail.com
#
#    Copyright (C) <2016>  <Ernesto Martinez Garcia>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#Advise: low quality code :)

from pyfiglet import Figlet
import os
import sys
from colorama import init
from colorama import Fore, Back, Style
import time
from random import randint

init()
print(Fore.MAGENTA)
random = (randint(0,3))
if (random == 0):
    f = Figlet(font='slant')
if (random == 1):
    f = Figlet(font='computer')
if (random == 2):
    f = Figlet(font='speed')
if (random == 3):
    f = Figlet(font='standard')
print f.renderText('ecomaikgolf')
print(Style.RESET_ALL)
init(autoreset=True)
print """
        #############################
        #                           #
        #  Automatically calculate  #
        #  Ruffini exact divisions  #
        #                           #
        #############################
"""
print(Fore.CYAN + '            Twitter: @ecomaikgolf')
print(Fore.CYAN + '           Ernesto Martinez Garcia')
print(Fore.CYAN + '           iecomaikgolf@gmail.com')
print""
print(Fore.YELLOW + """[-] This programm has been written with the 2.7 version of python
[-] Using version 3 could make this programm not work""")
grado = input(Fore.GREEN + '[+] Introduce polynom grade: ')
valores = [0] * 9
x = [0 for i in xrange(10)]
divisores=[]
if grado < 3:
    print(Fore.RED + '[!] Minimum grade is 3 :/')
    print(Fore.YELLOW + '[-] This can been caused by introducing non accepted characters like (Aa-Zz)')
    print(Fore.RED + '[!] Exiting...')
    sys.exit()
if grado > 7:
    print(Fore.RED + '[!] Max grade is 7 :/')
    print(Fore.YELLOW + '[-] This can been caused by introducing non accepted characters like (Aa-Zz)')
    print(Fore.RED + '[!] Exiting...')
    sys.exit()
i = 1
while (i <= grado):
    valores[i]=input(Fore.GREEN + '[+] Introduce a grade value: ')
    i = i + 1
ii = grado + 1
valores[ii]=input(Fore.GREEN + '[+] Introduce a value for the independent term: ')
if (grado == 3):
    print(Fore.CYAN + '[?] Values:'),valores[1],valores[2],valores[3],valores[4]
if (grado == 4):
    print(Fore.CYAN + '[?] Values:'),valores[1],valores[2],valores[3],valores[4],valores[5]
if (grado == 5):
    print(Fore.CYAN + '[?] Values:'),valores[1],valores[2],valores[3],valores[4],valores[5],valores[6]
if (grado == 6):
    print(Fore.CYAN + '[?] Values:'),valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7]
if (grado == 7):
    print(Fore.CYAN + '[?] Values:'),valores[1],valores[2],valores[3],valores[4],valores[5],valores[6],valores[7],valores[8]
time.sleep( 1 )
decision=raw_input(Fore.CYAN + '[?] Are values correct, from max exponent to minimum? s/n:')
if (decision == 'n'):
    print(Fore.RED + '[!] Exiting...')
    time.sleep( 2 )
    sys.exit()
if (decision == 'no'):
    time.sleep( 2 )
    print(Fore.RED + '[!] Exiting...')
    sys.exit()
time.sleep( 1 )
print(Fore.GREEN + '[+] Calculating...')
time.sleep( 1 )
print(Fore.YELLOW + '[-] The program is going to calculating by brute force, optimization is not implemented yet')
time.sleep( 2 )
print(Fore.YELLOW + '[-] This can take a while')
time.sleep( 3 )
o = grado + 1
la = 0
if(grado == 3):
    if(valores[o] > 0):
        l = 1
        while(l<=valores[o]):
            if(la == 1):
                break
                v = l*valores[1]
                v = v + valores[2]
                v = v * l
                v = v + valores[3]
                v = l * v
                v = v + valores[4]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l + 1
    if(valores[o] < 0):
        l = -1
        while(l>=valores[o]):
            if(la == 1):
                break
            v = l * valores[1]
            v = v + valores[2]
            v = v * l
            v = v + valores[3]
            v = l * v
            v = v + valores[4]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l - 1
    if(v!=0):
        print(Fore.RED + '[!] We cant find any exact divisor :/')
        print(Fore.RED + '[!] If you know this polynom has an exact divisor, report it to iecomaikgolf@gmail.com')
        print(Fore.RED + '[!] To avoid false errors, use the latest version of the program!')
if (grado == 4):
    if(valores[o] > 0):
        l = 1
        while(l<=valores[o]):
            if(la == 1):
                break
                v = l*valores[1]
                v = v + valores[2]
                v = v * l
                v = v + valores[3]
                v = l * v
                v = v + valores[4]
                v = l * v
                v = v + valores[5]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l + 1
    if(valores[o] < 0):
        l = -1
        while(l>=valores[o]):
            if(la == 1):
                break
            v = l * valores[1]
            v = v + valores[2]
            v = v * l
            v = v + valores[3]
            v = l * v
            v = v + valores[4]
            v = l * v
            v = v + valores[5]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l - 1
    if(v!=0):
        print(Fore.RED + '[!] We cant find any exact divisor :/')
        print(Fore.RED + '[!] If you know this polynom has an exact divisor, report it to iecomaikgolf@gmail.com')
        print(Fore.RED + '[!] To avoid false errors, use the latest version of the program!')
if (grado == 5):
    if(valores[o] > 0):
        l = 1
        while(l<=valores[o]):
            if(la == 1):
                break
                v = l*valores[1]
                v = v + valores[2]
                v = v * l
                v = v + valores[3]
                v = l * v
                v = v + valores[4]
                v = l * v
                v = v + valores[5]
                v = l * v
                v = v + valores[6]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            v = -l * v
            v = v + valores[6]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l + 1
    if(valores[o] < 0):
        l = -1
        while(l>=valores[o]):
            if(la == 1):
                break
            v = l * valores[1]
            v = v + valores[2]
            v = v * l
            v = v + valores[3]
            v = l * v
            v = v + valores[4]
            v = l * v
            v = v + valores[5]
            v = l * v
            v = v + valores[6]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            v = -l * v
            v = v + valores[6]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l - 1
    if(v!=0):
        print(Fore.RED + '[!] We cant find any exact divisor :/')
        print(Fore.RED + '[!] If you know this polynom has an exact divisor, report it to iecomaikgolf@gmail.com')
        print(Fore.RED + '[!] To avoid false errors, use the latest version of the program!')
if (grado == 6):
    if(valores[o] > 0):
        l = 1
        while(l<=valores[o]):
            if(la == 1):
                break
                v = l*valores[1]
                v = v + valores[2]
                v = v * l
                v = v + valores[3]
                v = l * v
                v = v + valores[4]
                v = l * v
                v = v + valores[5]
                v = l * v
                v = v + valores[6]
                v = l * v
                v = v + valores[7]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            v = -l * v
            v = v + valores[6]
            v = -l * v
            v = v + valores[7]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l + 1
    if(valores[o] < 0):
        l = -1
        while(l>=valores[o]):
            if(la == 1):
                break
            v = l * valores[1]
            v = v + valores[2]
            v = v * l
            v = v + valores[3]
            v = l * v
            v = v + valores[4]
            v = l * v
            v = v + valores[5]
            v = l * v
            v = v + valores[6]
            v = l * v
            v = v + valores[7]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            v = -l * v
            v = v + valores[6]
            v = -l * v
            v = v + valores[7]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l - 1
    if(v!=0):
        print(Fore.RED + '[!] We cant find any exact divisor :/')
        print(Fore.RED + '[!] If you know this polynom has an exact divisor, report it to iecomaikgolf@gmail.com')
        print(Fore.RED + '[!] To avoid false errors, use the latest version of the program!')
if (grado == 7):
    if(valores[o] > 0):
        l = 1
        while(l<=valores[o]):
            if(la == 1):
                break
                v = l*valores[1]
                v = v + valores[2]
                v = v * l
                v = v + valores[3]
                v = l * v
                v = v + valores[4]
                v = l * v
                v = v + valores[5]
                v = l * v
                v = v + valores[6]
                v = l * v
                v = v + valores[7]
                v = l * v
                v = v + valores[8]

            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            v = -l * v
            v = v + valores[6]
            v = -l * v
            v = v + valores[7]
            v = -l * v
            v = v + valores[8]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l + 1
    if(valores[o] < 0):
        l = -1
        while(l>=valores[o]):
            if(la == 1):
                break
            v = l * valores[1]
            v = v + valores[2]
            v = v * l
            v = v + valores[3]
            v = l * v
            v = v + valores[4]
            v = l * v
            v = v + valores[5]
            v = l * v
            v = v + valores[6]
            v = l * v
            v = v + valores[7]
            v = l * v
            v = v + valores[8]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),l
                la = 1
                break
            v = -l * valores[1]
            v = v + valores[2]
            v = v * -l
            v = v + valores[3]
            v = -l * v
            v = v + valores[4]
            v = -l * v
            v = v + valores[5]
            v = -l * v
            v = v + valores[6]
            v = -l * v
            v = v + valores[7]
            v = -l * v
            v = v + valores[8]
            if(v == 0):
                print(Fore.GREEN + '[+] The polynom is divisible by'),-l
                la = 1
                break
            l = l - 1
    if(v!=0):
        print(Fore.RED + '[!] We cant find any exact divisor :/')
        print(Fore.RED + '[!] If you know this polynom has an exact divisor, report it to iecomaikgolf@gmail.com')
        print(Fore.RED + '[!] To avoid false errors, use the latest version of the program!')
