1. Factoring
- Posted by Bryan Watts <BWatts1 at COMPUSERVE.COM> Aug 28, 1997
- 603 views
- Last edited Aug 29, 1997
Hey to all. I took high school Trigonometry / Pre-Calculus last year, and that's when I learned to program, on my TI-85. I used to write programs that would do all of my math for me, and just let me enter the problem. Well, if any of you can remember back that far, to factoring, I finally wrote a program to do it. Factoring, for those of you who forget, is where you get a problem like x=B2 - 4x - 12 and you have to come up with two things multiplied together that come to that, which is (x-6)(x+2) for that problem. Here's the code: ---------------------- include get.e include input.e --remember this? atom A,B,C puts(1,"Ax" & 253 & " + Bx + C") -- the little " =B2 " thingy A =3D input("A =3D ") B =3D input("B =3D ") C =3D input("C =3D ") function factor(atom b1,atom b2) for i =3D b1 - 100 to b1 + 100 do for j =3D b2 - 100 to b2 + 100 do if i + j =3D b1 and i * j =3D b2 then return {i,j} end if end for end for return {0} end function sequence junk junk =3D factor(B,C) printf(1,"(x + %d)(x + %d),{junk[1],junk[2]}) -------------------- This works great; I plugged in that problem earlier, and got the correct answer. I only have two problems: 1. I'm not sure what to do if the coefficient of the squared term is more than 1. 2. I can't handle really large numbers, since the for loops only go 100 in either direction. I know most of you either won't get this or won't care, but if there is a mathemetician out there who can help me, please do!! I'm entering 11th grade, Calculus Honors, and this program could really come in handy! Thanks for any help. Regards, Bryan Watts
2. Re: Factoring
- Posted by Daniel Berstein <danielberstein at USA.NET> Aug 30, 1997
- 600 views
> I know most of you either won't get this or won't care, > but if there is a mathemetician out there who can help me, > please do!! I'm entering 11th grade, Calculus Honors, > and this program could really come in handy! > Thanks for any help. Sorry for the long post and my bad mathematical english, but: 1.- I can't explain this in a shorter way, and 2.- I learnd this stuff in Spanish Ok, suppose you have the 3rd degree equation : 3x^3 - x + 2 = 0 You can write the coefficients of each degree of x as: (x^3) (x^2) (x^1) (x^0) 3 0 -1 2 If you find a number that multiplied by the the previous degree coeficient and then added to the next degree, recursivly until you get to x^0 and got 0 as a result, you have found a root of the equation. In the above example we could say ('k' is the number, root, we are looking): (3 + (-1 + (0 + (2*k))*k)*k) = 0 You can find that if k=-1 the above becomes true. This means that -1 is a root of the function and that (x - (k)), in this case (x+1), is a factor of it. You can write the above procedure as: k=-1 3 0 -1 2 => Coefficients ------------------------------------------------- -3 3 -2 => Product of last coefficient by k ------------------------------------------------- 3 -3 2 0 => Result of the adition As you can see, the last operation gives 0 as result, which means that (-1) is a root of the equation. Now look at the other results: 3 -3 2 ^^ ^^ ^^ x^2 x^1 x^0 This numbers gives you the other part of our factorized equation, so now we can certainly say that: 2x^3 - x + 2 = (x+1) * (3x^2 - 3x + 2) = 0 ^^^^^^^^^^ ^^^^ ^^^^^^^^^^^^ Original Root New coefficients You may ask how to get the value of 'k' without doing a loop (which is what your code does). There is a way: m = List of all the divisor of x^0 r = List of all the divisor of x^n (in this case x^3) Then the possible Real roots of the equation are in m/r. In this case: m = (+-)1, (+-)2 => divisors of 2 in 2x^0 r = (+-)1, (+-)3 => divisors of 3 in 3x^3 m/r = (+-)1/1, (+-)2/1, (+-)1/3, (+-)2/3 Then you only have to try with this 8 values (positive and negative). You can see that -1, which is a root of the equation, is contained in the list m/r. You can apply this method to the resulting polinomial, without recalculating m or r, the root (if not complex) will be contained in m/r.: m/r = (+-)1, (+-)2, (+-)1/3, (+-)2/3 3x^2 - 3x + 2 = 0 If you try this you'll find that (+-)1, (+-)2, (+-)1/3 or (+-)2/3 didn't satisfy the equation, which means that the 2 remaining roots are complex. This is consecuent with the second degree general equation: x^2 =(-b (+-) sqr(b^2 - 4*a*c)) / 2a which in this case end with (3 (+-) sqr(-15) ) / 6. That gives the complex roots: x1 = (1/3 + i*sqr(15)/6) x2 = (1/3 - i*sqr(15)/6) I hope you aren't very,very,very,very confused about this... Regards, Daniel Berstein danielberstein at usa.net http://www.geocities.com/SiliconValley/Heights/9316