Random Polynomial

This technique code example is a very simple random polynomial generator. The randPoly( function automatically creates a random polynomial of a given degree, however, the polynomial is not promised to be factorable. This routine will generate a factorable polynomial of a given degree using simple string manipulation.

The Code

factprac(deg)
Prgm
rand(deg)→b
"1"→str
For a,1,b
str&"(x+rand(20)-10)"→str
EndFor
Pause expand(expr(str)randPoly(x,deg-b))
EndPrgm

Explanation

The routine uses three variables, deg, b, and str. First, the user defines deg initially which is the degree of the random polynomial. Using the rand( command, the routine selects a number between one and deg. This is stored into b. This will be the number of rational roots the random polynomial will have.

The next part, including the For loop, is what makes the polynomial factorable. Here is the excerpt:

"1"→str
For a,1,b
str&"(x+rand(20)-10)"→str
EndFor

What we are wanting to do is create a string that represents the polynomial in factored form, for example (x+3)(x-2). We initialize the string (which is str) with a "1" since that will not interfere with the math later1. The For loop loops the number of rational roots we want determined by b. Each time the loop goes through, another factor is created and is combined with the current string with the & command.

Finally, it is time to display the creation. We use the Pause command because for higher degree polynomials, the resulting polynomial might run off the screen.

Pause expand(expr(str)randPoly(x,deg-b))

str is the string of factors, however, this number of factors will probably not be sufficient for the degree wanted since b might be less than deg. We therefore multiply the string with a random polynomial generated by randPoly( in order to achieve the desired degree. expr( turns the string into an expression that can be worked with. Finally, expand( will expand the factored polynomial into standard form.

The resulting polynomial can then be used for practice or something. The routine is meant to make a polynomial that can be factored, but perhaps not entirely factorable. If you want a program that generates a fully factorable polynomial, play with the code a little and try to figure it out!

Application

This routine demonstrates minor string manipulation. This shows how you can use various string commands, such as & and expr( in order to accomplish a task. It also demonstrates how to initialize a string depending on the purpose of the result.

Alternative

A downside to the above code is that it will only intentionally yield integer roots. The code below will also yield fractional rational roots, occasionally:

factprac(deg)
Prgm
"1"→str
rand(deg)→b
For a,1,b
If rand(10)-5≤0 Then
"("→c
Else
"((rand(5)+1)"→c
EndIf
str&c&"x+rand(20)-10)"→str
EndFor
Pause expand(expr(str)randPoly(x,deg-b))
EndPrgm
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License