Python Forum

Full Version: Factoring software
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on a program that can factor basic binomials. I am having trouble finding a way to verify if the result from 2 divided integers results in an integer. once I verify that, i then need to find a way to loop the code so that if it isn't an integer, it will restart the division with a new number, and if it is an integer, it will continue on with the program. (right now the program will get the guess right every now and then, as it runs off a random number generator to get the divisor, but it only gets it a small percentage of the time and i need a way for the program to automatically do this.)


print("Format - X + Ax + Bx ")
num1 = input(" input A value    - ")
num2 = input(" input B value    - ")
inum1 = int(num1)
inum2 = int(num2)
import random
divisor = int(random.randint(1,inum2))
value1 = inum2 / divisor
value2 = inum2/value1
if value1 + value2 == inum1:
    print( "(X " + "+ " + str(value1) + ") " + "(X " + "+ " + str(value2) + ")" )
Because there might be a little bit of precision error in division, you can check if something is an integer by rounding to an integer and see if it's within a small error of that rounding.

def is_integer(n):
    """Returns true if number is within certain precision of being an integer"""
    e = 1e-12
    return abs(n - round(n)) < e
I don't understand what you are trying to do. Could you provide an example with an answer and how that answer was derived please.