Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You haven't made it clear how big integers can be and still be small. The following code can easily be modified as needed

R.<x>=ZZ[]  #ring of polynomials with integral coefficients
f = R.random_element(2) #choose polynomial from the ring with degree at most 2
L = f.list() #get the coefficients
while L[2] != 1 or abs(L[1])>9 or abs(L[0])>10 or f.is_irreducible() == False:
    f = R.random_element(2)
    L = f.list()
print(f)

L[2] is the coefficient of x^2, so we insist that if it isn't 1 then we throw the polynomial away. We also throw the polynomial away if the coefficient of x in absolute value is >9 or the constant (in absolute value) is >10 or the polynomial is not irreducible.

You haven't made it clear how big integers can be and still be small. The following code can easily be modified as needed

R.<x>=ZZ[]  #ring of polynomials with integral coefficients
f = R.random_element(2) #choose polynomial from the ring with degree at most 2
L = f.list() #get the coefficients
while L[2] != 1 or abs(L[1])>9 or abs(L[0])>10 or f.is_irreducible() == False:
    f = R.random_element(2)
    L = f.list()
print(f)

L[2] is the coefficient of x^2, so we insist that if it isn't 1 then we throw the polynomial away. We also throw the polynomial away if the coefficient of x in absolute value is >9 or the constant (in absolute value) is >10 or the polynomial is not irreducible.

There's probably a less clunky way to do it but this works.

You haven't made it clear how big integers can be and still be small. The following code can easily be modified as needed

R.<x>=ZZ[]  #ring of polynomials with integral coefficients
f = R.random_element(2) #choose polynomial from the ring with degree at most 2
L = f.list() #get the coefficients
while L[2] != 1 or abs(L[1])>9 or abs(L[0])>10 or f.is_irreducible() == False:
    f = R.random_element(2)
    L = f.list()
print(f)

L[2] is the coefficient of x^2, so we insist that if it isn't 1 then we throw the polynomial away. We also throw the polynomial away if the coefficient of x in absolute value is >9 or the constant (in absolute value) is >10 or the polynomial is not irreducible.

There's probably a less clunky way to do it but this works.

EDIT: following rburing comment below will mean not having to throw out so many polynomials for not being monic.

R.<x>=ZZ[]  #ring of polynomials with integral coefficients
poly = R.random_element(1) #choose polynomial from the ring with degree at most 1
f = x^2+poly
L = f.list() #get the coefficients
while abs(L[1])>9 or abs(L[0])>10 or f.is_irreducible() == False:
    poly = R.random_element(1)
    f = x^2+poly
    L = f.list()
print(f)