Jump to content

Quadratic solver


Mat
 Share

Recommended Posts

very nice, thx. i wish my son would script something like this instead of WoW....

this reminds me of a long time planned project of mine: a universal calculator with math plugins by anyone who wants to contribute.... some time maybe.

keep on !

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

Search for Project Euler on here. Many problems there involved factorization... :)

?? I'll have a look but I mean factorizing like:

x^2 + x - 20 = 0
(x + 5)(x - 4) = 0

therefore
x1 = -5
x2 = 4

I don't think i saw that on project euler (at least the ones we did in autoit) you should also remember I own that project page on google ;)

Mat

Link to comment
Share on other sites

Just mathly speaking.. If both your x's are integers, than you could just say you factored it.

E.g. x^2 + 7x + 12 works out by the formula to be x=3 and x=4. Therefore: (x-3)(x-4) is your factored equation.

That's a kind of ghetto way of doing it.. but it works. I haven't thought of the negatives of using this...

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Just mathly speaking.. If both your x's are integers, than you could just say you factored it.

E.g. x^2 + 7x + 12 works out by the formula to be x=3 and x=4. Therefore: (x-3)(x-4) is your factored equation.

That's a kind of ghetto way of doing it.. but it works. I haven't thought of the negatives of using this...

Thats really clever. As in... Very very very clever. Defeats the point ever so slightly but very clever.

Thanks a lot achilles i'll put that in the next version with a big shiny badge for you :)

Mat

Link to comment
Share on other sites

Thats really clever. As in... Very very very clever. Defeats the point ever so slightly but very clever.

Thanks a lot achilles i'll put that in the next version with a big shiny badge for you :)

Mat

lol, thanks... I'm glad all my high school and college education has paid off somewhere.
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Only problem is when A is not 1. Thats the only reason I haven't released it yet.

10x^2 + 13x - 30

(2x + 5)(5x - 6)

This can't be factorised by my program as it does not return integer results (1.2 and -2.5 I think...)

Mat

Edit: More replies than downloads. Doesn't say much for the program :)

Edited by Mat
Link to comment
Share on other sites

Only problem is when A is not 1. Thats the only reason I haven't released it yet.

10x^2 + 13x - 30

(2x + 5)(5x - 6)

This can't be factorised by my program as it does not return integer results (1.2 and -2.5 I think...)

Mat

Edit: More replies than downloads. Doesn't say much for the program :)

Ok this adds a new element, but the problem can be broken down. You need to test all factors of A against all factors of C while trying to derive B

Call the factors of A... a1 and a2

And the factors of C... c1 and c2

Now test all factors of A against all factors of C until you find a match for the following equation:

a1*c2 + a2*c1 = B

And the resulting factorization will be:

(a1x + c1)(a2x + c2)

Edited by czardas
Link to comment
Share on other sites

Only problem is when A is not 1. Thats the only reason I haven't released it yet.

10x^2 + 13x - 30

(2x + 5)(5x - 6)

This can't be factorised by my program as it does not return integer results (1.2 and -2.5 I think...)

Mat

Edit: More replies than downloads. Doesn't say much for the program :)

I knew there was something too simple about my solution... Maybe check rational numbers to see if they are something simple (e.g. .2 or .5 or .3333333333333..) and if they are just add the necessary coefficients) I don't know, that's still doing stuff the ghetto way.
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

here's a neat trick

called rational root test

if you have a_n*x^n + a_n-1 * x^n-1 + ... + a_1*x + a_0

(in this case just ax^2 + bx + c)

then the only possible rational roots are +-p/q

where p and q are integers dividing a_0 and a_n respectively

not super useful but nice to know and can be adapted for finding rational solutions to this

now for your problem at hand

suppose you know

ax^2 + bx + c

factors as

(dx+e)(fx+g)

then working backwards you can do the following:

ax^2 + bx + c

=

ax^2 + (ef)x + (dg)x + c

=

fx(dx+e) + g(dx+e)

=

(dx+e)(fx+g)

which is how a student might be expected to show work without using the quadratic formula

all that's left for you is to be able to take a fraction and express it as it's numerator and denominator

Link to comment
Share on other sites

Mat

You inspired me to try this.

A Mini Quadratic Solver based on the formula, Posted Image

$x  = InputBox("Values of X", 'Example entry "1x^2 + 0x - 9"', "1x^2 + 0x - 9", "", 50, 50, 150, 100)
$ans = Execute(StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "((-(${3})+(((${3})*(${3}))-(4*${1}*(${5})))^0.5)/(2*${1}))"))
$ans1 = Execute(StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "((-(${3})-(((${3})*(${3}))-(4*${1}*(${5})))^0.5)/(2*${1}))"))
MsgBox(0, "Results", 'For "' & $x & ' = 0"' & @CRLF & @CRLF & "x = " & $ans & @CRLF & " or " & @CRLF & "x = " & $ans1)
Link to comment
Share on other sites

Yep... Now put it into practice... Its harder than you make it sound.

Thanks a lot though, its helping me to understand how it works as well as getting this program written.

Mat

Ok, here's an attempt (written in AutoIt Version: 3.2.6.0) I guess there's no difference in this particular script.

#include <Array.au3>

Dim $arrayA[1], $arrayC[1], $A = 10, $B = 13, $C = -30 ;Values from the example above

$P = $A ; Patch added to negate final expression (not ideal)

If $A < 0 Then ; Force a positive coefficient for x^2
    $A = -$A
    $B = -$B
    $C = -$C
EndIf

For $i = 1 To Int($A^.5) Step 1 ;List divisors for $A
    If Mod($A, $i) = 0 Then
        _ArrayAdd($arrayA,$i&","&$A/$i)
    EndIf
Next

$numC = $C
If $C < 0 Then ; Might as well use positive integers to determine the divisors
    $numC = -$C
EndIf
For $i = 1 To Int($numC^.5) Step 1 ;List divisors for $numC
    If Mod($numC, $i) = 0 Then
        _ArrayAdd($arrayC,$i&","&$numC/$i)
    EndIf
Next

$factorization = "Could not factorize the expression!"

For $i = 1 To UBound($arrayA) -1 Step 1 ; Test each pair of divisors from $arrayA
    $divsA = StringSplit($arrayA[$i], ",")
    For $j = 1 To UBound($arrayC) -1 Step 1 ; Tested against each pair of divisors from $arrayC
        $divsC = StringSplit($arrayC[$j], ",")
        If $C > 0 Then ; divsC have the same sign
            If $divsA[1]*$divsC[2] + $divsA[2]*$divsC[1] = $B Then
                $factorization = "("&$divsA[1]&"x + "&$divsC[1]&")("&$divsA[2]&"x + "&$divsC[2]&")" ;(x + n)(x + n)
            ElseIf $divsA[1]*$divsC[2] + $divsA[2]*$divsC[1] = -$B Then
                $factorization = "("&$divsA[1]&"x - "&$divsC[1]&")("&$divsA[2]&"x - "&$divsC[2]&")" ;(x - n)(x - n)
            EndIf
        ElseIf $C < 0 Then ; divsC have opposite signs
            If $divsA[1]*$divsC[2] - $divsA[2]*$divsC[1] = $B Then
                $factorization = "("&$divsA[1]&"x - "&$divsC[1]&")("&$divsA[2]&"x + "&$divsC[2]&")"  ;(x - n)(x + n)
            ElseIf $divsA[2]*$divsC[1] - $divsA[1]*$divsC[2] = $B Then
                $factorization = "("&$divsA[1]&"x + "&$divsC[1]&")("&$divsA[2]&"x - "&$divsC[2]&")"  ;(x + n)(x - n)
            EndIf
        EndIf
    Next
Next

If $P < 0 Then ; Patch added to negate final expression (not ideal)
    $factorization = "-"&$factorization
EndIf

MsgBox(0, "Factorization", $factorization)

I'm sure the script can probably be simplified or improved in other ways. It also needs testing for different values of $A, $B and $C. Perhaps I made a mistake. This script does not cater for coefficient values of zero. Those variations should be treated as special cases.

Edit1: I forgot to escape the loop when a solution is found. Return values of '1x' can be substituted for plain 'x', but I'm too tired to alter it right now.

Edit2: After looking at this again, I notice that I also forgot to negate the final expression when $A starts out with a negative value. :) This is required because I forced $A to have a positive value. Perhaps this is the wrong approach. I have added a temporary patch for the time being (4 lines). This is not a solution! I will have to rewrite the code (as it's now become far too messy) and include factorization for when coefficients of A, B or C are equal to zero.

Edited by czardas
Link to comment
Share on other sites

Surely if you are multiplying by -1 thoughout then it does not matter.

2x^2 + 4x - 5 = 0

is the same as

-2x^2 - 4x + 5 = 0

2x^2 + 4x - 5 = -2x^2 - 4x + 5
2x^2 + 4x = -2x^2 - 4x + 10
x^2 + 2x = -(x^2) - 2x + 5
2x^2 + 4x - 5 = 0
(and I ever doubted it... ;) )

So I'm not sure what your referring to there.

I had written a nice long reply to everyone else, but it got eaten by the big forum monster, and I seriously can't be asked to write it all out again. basically, Well done everyone :).

New version is due to be released in 10 secs and counting. Thanks a lot to the work of czardas, the final code for factorizing is based very much on what he's done above, with me polishing it off ever so slightly.

Mat

Link to comment
Share on other sites

Surely if you are multiplying by -1 thoughout then it does not matter.

Mat

Hmm yes! Well I think that's only true providing that the expression equals zero; which is true in this case, so just forget the 4 lines that I added, which are not a good solution anyway. However, you might want to consider scenarios where the user wishes to factorize without knowing the value of the expression. Perhaps that's something for a future release. Still, I'm glad that the above code was helpful to you. :) Edited by czardas
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...