1

I'm trying to write a program that will factor any trinomial of the form ax^2+bx+c, but I'm kind of stuck. I've got a sequence of 4 for loops that looks like this:

print "Factoring...\n" for i in range(low_value, high_value): for j in range(low_value, high_value): for k in range(low_value, high_value): for l in range(low_value, high_value): print "testing\n" if i*k==a & j*l==c & (i*l)+(j*k)==b: print "Your factored Equation is: (" + i + "x + " + j + ")(" + k + "x + " + l + ")" else: print "No solution found.\n" print "testing...\n" 

Anyways I know the code is far from optimal but nothing inside the for loops are executing. The "testing..." message at the end is displayed but nothing is printed before that(by that I mean the "testing", "Your factored equation is:", or the "No solution found"). Is there some kind of limitation in Python where I can't use so many for loops at once? Is there something wrong with my syntax that I just am unable to see? Any help would be greatly appreciated :)

7
  • How do you define low_value and high_value? Commented Aug 21, 2012 at 17:42
  • 1
    If you assign a value to low_value and high_value, your code does in fact do print "testing", so there doesn't seem to be any problem with the loops. Commented Aug 21, 2012 at 17:42
  • 1
    Syntax-wise, you should use the and keyword in place of & in your if statement. Commented Aug 21, 2012 at 17:43
  • 1
    You haven't posted the code which prompts for the input. I'd guess something weird is going on there. Commented Aug 21, 2012 at 17:47
  • 2
    As an aside, are you familiar with the quadratic formula? This factoring can be done without any loops, which will make it far faster. Commented Aug 21, 2012 at 18:09

3 Answers 3

3

You have to use and for logical conjunction, not '&' symbol. Try this:

... if (i*k==a) and (j*l==c) and ((i*l+j*k)==b): ... 
Sign up to request clarification or add additional context in comments.

7 Comments

this is just a "better programming practice" not a syntax error
@SpiXel: While it isn't a syntax error it still is an error, since & is bitwise and.
@SpiXel: Not only is & bitwise instead of Boolean, it has a higher precedence than ==, whereas and has a lower precedence. At the Python prompt, try 4==4 & 3==3 and then try 4==4 and 3==3.
@sth yes you're right , but still , considering boolean values being checked , is there still any differences ?
@JohnY That precedence does it, so it makes a difference there. Thanks
|
2

Apart from the question stated, there are a few problems with the code: :-)

  • Given low_value of -1000 and high_value of 1000, "testing" and "no solution has been found" will be printed ~16 trillion times.
  • Execution doesn't stop when a solution is found.
  • print as statement restricts the code to Python2, it is recommended to use the print(stuff) function instead.
  • print automatically inserts a newline. \n is not needed.

Here's a suggested rewrite:

def factor(a, b, c): low_value = min(a, b, c) high_value = max(a, b, c) for i in range(low_value, high_value): for j in range(low_value, high_value): for k in range(low_value, high_value): if i*k != a: # check this clause earlier to improve speed a bit continue for l in range(low_value, high_value): if j*l != c: continue if (i*l) + (j*k) != b: continue return (i, j, k, l) return None print("Factoring...") solution = factor(1, 2, 4) if solution: (i, j, k, l) = solution print("Your factored Equation is: (" + i + "x + " + j + ")" + "(" + k + "x + " + l + ")") else: print("No solution can be found.") 

2 Comments

Yeah I hadn't quite gotten all the parts of the code in the right place yet, I just wanted to make sure all the logic was working properly before. But thanks a lot! :)
@StevenRumbalski it's a typo for "None". Derp.
0

To make it more pythonic:

from itertools import product for i, j, k, l in product(range(low, high), repeat=4): print i, j, k, l 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.