Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factoring software
#1
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) + ")" )
buran write Dec-06-2020, 07:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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
Walkerh163 likes this post
Reply
#3
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020