Find “prime polynomials” for a user's given prime, bounds, and degree
$begingroup$
Very amateur programmer and first-time poster here.
The program I wrote asks the user for a prime number, a lower and upper bound, and a degree (all of these are integers). I want to generate a list of polynomials, represented by its coefficients (see example). Each polynomial $P(x)$ satisfies the following conditions:
$P(x) + p$ is prime for $1 le x le p - 2$ (where $x$ is an integer), and $P(0) = 0$.
Each coefficient is an integer and is confined by the user's bounds inclusively (i.e., the coefficient may equal a bound but may not be less/greater than it).
The highest degree a polynomial can have is the user's given degree. (The user may choose to include just polynomials with this degree or polynomials with lower degrees in the final list).
The program then finds each polynomial with these conditions and prints a list of primes that it generates.
Example:
Enter the prime number you want to find a poly for: 11
Enter the lower bound: -3
Enter the higher bound: 3
Enter the degree of the polynomial: 3
Press n if you do not want to include lower degree polynomials: n
possible combos (including constant func):
215
################################################
poly generating finished
[[11, -2, 0, 2]]
List of primes that [11, -2, 0, 2] generates:
[11, 11, 23, 59, 131, 251, 431, 683, 1019, 1451]
There are 1 good polynomials for 11 with bounds -3 to 3 inclusive up to degree 3
Here, $[11, -2, 0, 2]$ represents $p=11$ with the polynomial $- 2x + 2x^3$.
The general idea is that we start with a polynomial where every coefficient is the lower bound, check if the polynomial is a "good" or "prime" polynomial (satisfies the first condition), and add it to the list of prime polynomials if it is. Repeat with the next polynomial (list of numbers) until every combination has been exhausted.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
if not isPrime(polyValue):
return False
return True
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
allCombosNum = (abs(HB - LB))**deg - 1
#creates list of all possible tuples that represent a poly
print("possible combos (including constant func): ")
print(allCombosNum)
goodPolyList =
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
for x in range(allCombosNum):
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
#personal usage: keeps track of how many more combos it needs to go through
numLeft = allCombosNum - x
if (numLeft % 100000) == 0:
print(numLeft)
#create the next combo
combo = genCombo(combo, LB, HB)
print("################################################")
print("poly generating finished")
print()
print(goodPolyList)
#bonus stuff
#goes over items in the goodPolyList and shows what primes each generates
for item in goodPolyList:
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
print()
print("List of primes that" , item, "generates: ")
print(primeList)
print()
print("There are" , len(goodPolyList) , "good polynomials for", prime ,
"with bounds" , LB , " to" , HB, "inclusive up to degree" , deg)
verifyPrime()
verifyPrime()
(As you see I've used a couple snippets of code from stackoverflow. Admittedly this is for simplicity's sake as I don't quite understand them.)
I am mostly concerned with speed since I intend to go through a very high amount of polynomials. However, since I am still very new at this, any feedback is appreciated, particularly in keeping code clean, comments/variable names -- basic stuff (but again, any feedback is fine). If it matters, this code will be for my personal use and not for any school assignment/project.
python beginner time-limit-exceeded primes mathematics
$endgroup$
add a comment |
$begingroup$
Very amateur programmer and first-time poster here.
The program I wrote asks the user for a prime number, a lower and upper bound, and a degree (all of these are integers). I want to generate a list of polynomials, represented by its coefficients (see example). Each polynomial $P(x)$ satisfies the following conditions:
$P(x) + p$ is prime for $1 le x le p - 2$ (where $x$ is an integer), and $P(0) = 0$.
Each coefficient is an integer and is confined by the user's bounds inclusively (i.e., the coefficient may equal a bound but may not be less/greater than it).
The highest degree a polynomial can have is the user's given degree. (The user may choose to include just polynomials with this degree or polynomials with lower degrees in the final list).
The program then finds each polynomial with these conditions and prints a list of primes that it generates.
Example:
Enter the prime number you want to find a poly for: 11
Enter the lower bound: -3
Enter the higher bound: 3
Enter the degree of the polynomial: 3
Press n if you do not want to include lower degree polynomials: n
possible combos (including constant func):
215
################################################
poly generating finished
[[11, -2, 0, 2]]
List of primes that [11, -2, 0, 2] generates:
[11, 11, 23, 59, 131, 251, 431, 683, 1019, 1451]
There are 1 good polynomials for 11 with bounds -3 to 3 inclusive up to degree 3
Here, $[11, -2, 0, 2]$ represents $p=11$ with the polynomial $- 2x + 2x^3$.
The general idea is that we start with a polynomial where every coefficient is the lower bound, check if the polynomial is a "good" or "prime" polynomial (satisfies the first condition), and add it to the list of prime polynomials if it is. Repeat with the next polynomial (list of numbers) until every combination has been exhausted.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
if not isPrime(polyValue):
return False
return True
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
allCombosNum = (abs(HB - LB))**deg - 1
#creates list of all possible tuples that represent a poly
print("possible combos (including constant func): ")
print(allCombosNum)
goodPolyList =
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
for x in range(allCombosNum):
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
#personal usage: keeps track of how many more combos it needs to go through
numLeft = allCombosNum - x
if (numLeft % 100000) == 0:
print(numLeft)
#create the next combo
combo = genCombo(combo, LB, HB)
print("################################################")
print("poly generating finished")
print()
print(goodPolyList)
#bonus stuff
#goes over items in the goodPolyList and shows what primes each generates
for item in goodPolyList:
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
print()
print("List of primes that" , item, "generates: ")
print(primeList)
print()
print("There are" , len(goodPolyList) , "good polynomials for", prime ,
"with bounds" , LB , " to" , HB, "inclusive up to degree" , deg)
verifyPrime()
verifyPrime()
(As you see I've used a couple snippets of code from stackoverflow. Admittedly this is for simplicity's sake as I don't quite understand them.)
I am mostly concerned with speed since I intend to go through a very high amount of polynomials. However, since I am still very new at this, any feedback is appreciated, particularly in keeping code clean, comments/variable names -- basic stuff (but again, any feedback is fine). If it matters, this code will be for my personal use and not for any school assignment/project.
python beginner time-limit-exceeded primes mathematics
$endgroup$
$begingroup$
How does P(x) = 11 - 2x + 2x^3 satisfy the P(0) = 0 requirement?
$endgroup$
– 200_success
Dec 12 '18 at 14:46
$begingroup$
Sorry, I suppose I wasn't very clear on that. -2x + 2x^3 is P(x) and 11 is the prime p.
$endgroup$
– Gizmo
Dec 12 '18 at 22:42
add a comment |
$begingroup$
Very amateur programmer and first-time poster here.
The program I wrote asks the user for a prime number, a lower and upper bound, and a degree (all of these are integers). I want to generate a list of polynomials, represented by its coefficients (see example). Each polynomial $P(x)$ satisfies the following conditions:
$P(x) + p$ is prime for $1 le x le p - 2$ (where $x$ is an integer), and $P(0) = 0$.
Each coefficient is an integer and is confined by the user's bounds inclusively (i.e., the coefficient may equal a bound but may not be less/greater than it).
The highest degree a polynomial can have is the user's given degree. (The user may choose to include just polynomials with this degree or polynomials with lower degrees in the final list).
The program then finds each polynomial with these conditions and prints a list of primes that it generates.
Example:
Enter the prime number you want to find a poly for: 11
Enter the lower bound: -3
Enter the higher bound: 3
Enter the degree of the polynomial: 3
Press n if you do not want to include lower degree polynomials: n
possible combos (including constant func):
215
################################################
poly generating finished
[[11, -2, 0, 2]]
List of primes that [11, -2, 0, 2] generates:
[11, 11, 23, 59, 131, 251, 431, 683, 1019, 1451]
There are 1 good polynomials for 11 with bounds -3 to 3 inclusive up to degree 3
Here, $[11, -2, 0, 2]$ represents $p=11$ with the polynomial $- 2x + 2x^3$.
The general idea is that we start with a polynomial where every coefficient is the lower bound, check if the polynomial is a "good" or "prime" polynomial (satisfies the first condition), and add it to the list of prime polynomials if it is. Repeat with the next polynomial (list of numbers) until every combination has been exhausted.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
if not isPrime(polyValue):
return False
return True
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
allCombosNum = (abs(HB - LB))**deg - 1
#creates list of all possible tuples that represent a poly
print("possible combos (including constant func): ")
print(allCombosNum)
goodPolyList =
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
for x in range(allCombosNum):
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
#personal usage: keeps track of how many more combos it needs to go through
numLeft = allCombosNum - x
if (numLeft % 100000) == 0:
print(numLeft)
#create the next combo
combo = genCombo(combo, LB, HB)
print("################################################")
print("poly generating finished")
print()
print(goodPolyList)
#bonus stuff
#goes over items in the goodPolyList and shows what primes each generates
for item in goodPolyList:
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
print()
print("List of primes that" , item, "generates: ")
print(primeList)
print()
print("There are" , len(goodPolyList) , "good polynomials for", prime ,
"with bounds" , LB , " to" , HB, "inclusive up to degree" , deg)
verifyPrime()
verifyPrime()
(As you see I've used a couple snippets of code from stackoverflow. Admittedly this is for simplicity's sake as I don't quite understand them.)
I am mostly concerned with speed since I intend to go through a very high amount of polynomials. However, since I am still very new at this, any feedback is appreciated, particularly in keeping code clean, comments/variable names -- basic stuff (but again, any feedback is fine). If it matters, this code will be for my personal use and not for any school assignment/project.
python beginner time-limit-exceeded primes mathematics
$endgroup$
Very amateur programmer and first-time poster here.
The program I wrote asks the user for a prime number, a lower and upper bound, and a degree (all of these are integers). I want to generate a list of polynomials, represented by its coefficients (see example). Each polynomial $P(x)$ satisfies the following conditions:
$P(x) + p$ is prime for $1 le x le p - 2$ (where $x$ is an integer), and $P(0) = 0$.
Each coefficient is an integer and is confined by the user's bounds inclusively (i.e., the coefficient may equal a bound but may not be less/greater than it).
The highest degree a polynomial can have is the user's given degree. (The user may choose to include just polynomials with this degree or polynomials with lower degrees in the final list).
The program then finds each polynomial with these conditions and prints a list of primes that it generates.
Example:
Enter the prime number you want to find a poly for: 11
Enter the lower bound: -3
Enter the higher bound: 3
Enter the degree of the polynomial: 3
Press n if you do not want to include lower degree polynomials: n
possible combos (including constant func):
215
################################################
poly generating finished
[[11, -2, 0, 2]]
List of primes that [11, -2, 0, 2] generates:
[11, 11, 23, 59, 131, 251, 431, 683, 1019, 1451]
There are 1 good polynomials for 11 with bounds -3 to 3 inclusive up to degree 3
Here, $[11, -2, 0, 2]$ represents $p=11$ with the polynomial $- 2x + 2x^3$.
The general idea is that we start with a polynomial where every coefficient is the lower bound, check if the polynomial is a "good" or "prime" polynomial (satisfies the first condition), and add it to the list of prime polynomials if it is. Repeat with the next polynomial (list of numbers) until every combination has been exhausted.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
if not isPrime(polyValue):
return False
return True
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
allCombosNum = (abs(HB - LB))**deg - 1
#creates list of all possible tuples that represent a poly
print("possible combos (including constant func): ")
print(allCombosNum)
goodPolyList =
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
for x in range(allCombosNum):
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
#personal usage: keeps track of how many more combos it needs to go through
numLeft = allCombosNum - x
if (numLeft % 100000) == 0:
print(numLeft)
#create the next combo
combo = genCombo(combo, LB, HB)
print("################################################")
print("poly generating finished")
print()
print(goodPolyList)
#bonus stuff
#goes over items in the goodPolyList and shows what primes each generates
for item in goodPolyList:
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
print()
print("List of primes that" , item, "generates: ")
print(primeList)
print()
print("There are" , len(goodPolyList) , "good polynomials for", prime ,
"with bounds" , LB , " to" , HB, "inclusive up to degree" , deg)
verifyPrime()
verifyPrime()
(As you see I've used a couple snippets of code from stackoverflow. Admittedly this is for simplicity's sake as I don't quite understand them.)
I am mostly concerned with speed since I intend to go through a very high amount of polynomials. However, since I am still very new at this, any feedback is appreciated, particularly in keeping code clean, comments/variable names -- basic stuff (but again, any feedback is fine). If it matters, this code will be for my personal use and not for any school assignment/project.
python beginner time-limit-exceeded primes mathematics
python beginner time-limit-exceeded primes mathematics
edited Dec 12 '18 at 23:22
200_success
129k15153415
129k15153415
asked Dec 12 '18 at 6:28
GizmoGizmo
1433
1433
$begingroup$
How does P(x) = 11 - 2x + 2x^3 satisfy the P(0) = 0 requirement?
$endgroup$
– 200_success
Dec 12 '18 at 14:46
$begingroup$
Sorry, I suppose I wasn't very clear on that. -2x + 2x^3 is P(x) and 11 is the prime p.
$endgroup$
– Gizmo
Dec 12 '18 at 22:42
add a comment |
$begingroup$
How does P(x) = 11 - 2x + 2x^3 satisfy the P(0) = 0 requirement?
$endgroup$
– 200_success
Dec 12 '18 at 14:46
$begingroup$
Sorry, I suppose I wasn't very clear on that. -2x + 2x^3 is P(x) and 11 is the prime p.
$endgroup$
– Gizmo
Dec 12 '18 at 22:42
$begingroup$
How does P(x) = 11 - 2x + 2x^3 satisfy the P(0) = 0 requirement?
$endgroup$
– 200_success
Dec 12 '18 at 14:46
$begingroup$
How does P(x) = 11 - 2x + 2x^3 satisfy the P(0) = 0 requirement?
$endgroup$
– 200_success
Dec 12 '18 at 14:46
$begingroup$
Sorry, I suppose I wasn't very clear on that. -2x + 2x^3 is P(x) and 11 is the prime p.
$endgroup$
– Gizmo
Dec 12 '18 at 22:42
$begingroup$
Sorry, I suppose I wasn't very clear on that. -2x + 2x^3 is P(x) and 11 is the prime p.
$endgroup$
– Gizmo
Dec 12 '18 at 22:42
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Firstly, on documentation: the standard term for the object you're searching for is prime-generating polynomial. "Prime polynomial" is often used as a synonym for "irreducible polynomial", and while there is a relationship between reducibility and generation of primes it's best to use standard terms in documentation where possible.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
This looks a bit untidy. It's not very Pythonic to put multiple statements on a line separated by ;
. The import itertools
is unnecessary, because you explicitly import all of the itertools methods that you use. The two from itertools import
statements can be combined into one.
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
This is a reasonable way to check a single number for primality if the number isn't too large, but since you're checking lots of numbers and you mention this as a performance concern, I would suggest that you think about building a sieve of Eratosphenes for smallish numbers (up to say 10 million or 100 million) and using probabilistic primality testing for numbers larger than that. Perhaps BPSW.
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
The name suggests a search, but it's actually an evaluation. I'd call it something like evalPoly(coeffs, x)
. The evaluation can be made more efficient using Horner's method, which can be written as a reduce
call.
It would be worth adding a docstring to document the order of the coefficients: constant term first (a_0, ..., a_n
) or last (a_n, ..., a_0
).
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
Why is this duplicating the contents of findSingleValue
rather than calling it?
if not isPrime(polyValue):
return False
return True
Why not use all(...)
?
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
I'd half expect permtools
to have a built-in method for this. Alternatively it can be done with (untested code) itertools.product(range(LB, HB+1), deg)
.
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
This could use a refactor: one method to do the work, and then the main method just does the I/O.
allCombosNum = (abs(HB - LB))**deg - 1
I think this has an out-by-one error.
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
My suggestion above would make this unnecessary, but... tuple(repeat(LB, deg))
?
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
I think this is polyList = [prime] + list(combo)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
There's a potential performance improvement here. If lowDegPoly == "n"
then it's more efficient to avoid generating and testing polynomials of lower degree.
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
primeList = [findSingleValue(item, x) for x in range(prime - 1)]
verifyPrime()
verifyPrime()
That recursive call is rather inelegant, and the direct invocation of the main method is not considered best practice. It would be better to replace these lines with
if __name__ == "__main__":
while True:
verifyPrime()
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209498%2ffind-prime-polynomials-for-a-users-given-prime-bounds-and-degree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Firstly, on documentation: the standard term for the object you're searching for is prime-generating polynomial. "Prime polynomial" is often used as a synonym for "irreducible polynomial", and while there is a relationship between reducibility and generation of primes it's best to use standard terms in documentation where possible.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
This looks a bit untidy. It's not very Pythonic to put multiple statements on a line separated by ;
. The import itertools
is unnecessary, because you explicitly import all of the itertools methods that you use. The two from itertools import
statements can be combined into one.
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
This is a reasonable way to check a single number for primality if the number isn't too large, but since you're checking lots of numbers and you mention this as a performance concern, I would suggest that you think about building a sieve of Eratosphenes for smallish numbers (up to say 10 million or 100 million) and using probabilistic primality testing for numbers larger than that. Perhaps BPSW.
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
The name suggests a search, but it's actually an evaluation. I'd call it something like evalPoly(coeffs, x)
. The evaluation can be made more efficient using Horner's method, which can be written as a reduce
call.
It would be worth adding a docstring to document the order of the coefficients: constant term first (a_0, ..., a_n
) or last (a_n, ..., a_0
).
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
Why is this duplicating the contents of findSingleValue
rather than calling it?
if not isPrime(polyValue):
return False
return True
Why not use all(...)
?
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
I'd half expect permtools
to have a built-in method for this. Alternatively it can be done with (untested code) itertools.product(range(LB, HB+1), deg)
.
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
This could use a refactor: one method to do the work, and then the main method just does the I/O.
allCombosNum = (abs(HB - LB))**deg - 1
I think this has an out-by-one error.
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
My suggestion above would make this unnecessary, but... tuple(repeat(LB, deg))
?
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
I think this is polyList = [prime] + list(combo)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
There's a potential performance improvement here. If lowDegPoly == "n"
then it's more efficient to avoid generating and testing polynomials of lower degree.
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
primeList = [findSingleValue(item, x) for x in range(prime - 1)]
verifyPrime()
verifyPrime()
That recursive call is rather inelegant, and the direct invocation of the main method is not considered best practice. It would be better to replace these lines with
if __name__ == "__main__":
while True:
verifyPrime()
$endgroup$
add a comment |
$begingroup$
Firstly, on documentation: the standard term for the object you're searching for is prime-generating polynomial. "Prime polynomial" is often used as a synonym for "irreducible polynomial", and while there is a relationship between reducibility and generation of primes it's best to use standard terms in documentation where possible.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
This looks a bit untidy. It's not very Pythonic to put multiple statements on a line separated by ;
. The import itertools
is unnecessary, because you explicitly import all of the itertools methods that you use. The two from itertools import
statements can be combined into one.
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
This is a reasonable way to check a single number for primality if the number isn't too large, but since you're checking lots of numbers and you mention this as a performance concern, I would suggest that you think about building a sieve of Eratosphenes for smallish numbers (up to say 10 million or 100 million) and using probabilistic primality testing for numbers larger than that. Perhaps BPSW.
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
The name suggests a search, but it's actually an evaluation. I'd call it something like evalPoly(coeffs, x)
. The evaluation can be made more efficient using Horner's method, which can be written as a reduce
call.
It would be worth adding a docstring to document the order of the coefficients: constant term first (a_0, ..., a_n
) or last (a_n, ..., a_0
).
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
Why is this duplicating the contents of findSingleValue
rather than calling it?
if not isPrime(polyValue):
return False
return True
Why not use all(...)
?
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
I'd half expect permtools
to have a built-in method for this. Alternatively it can be done with (untested code) itertools.product(range(LB, HB+1), deg)
.
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
This could use a refactor: one method to do the work, and then the main method just does the I/O.
allCombosNum = (abs(HB - LB))**deg - 1
I think this has an out-by-one error.
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
My suggestion above would make this unnecessary, but... tuple(repeat(LB, deg))
?
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
I think this is polyList = [prime] + list(combo)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
There's a potential performance improvement here. If lowDegPoly == "n"
then it's more efficient to avoid generating and testing polynomials of lower degree.
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
primeList = [findSingleValue(item, x) for x in range(prime - 1)]
verifyPrime()
verifyPrime()
That recursive call is rather inelegant, and the direct invocation of the main method is not considered best practice. It would be better to replace these lines with
if __name__ == "__main__":
while True:
verifyPrime()
$endgroup$
add a comment |
$begingroup$
Firstly, on documentation: the standard term for the object you're searching for is prime-generating polynomial. "Prime polynomial" is often used as a synonym for "irreducible polynomial", and while there is a relationship between reducibility and generation of primes it's best to use standard terms in documentation where possible.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
This looks a bit untidy. It's not very Pythonic to put multiple statements on a line separated by ;
. The import itertools
is unnecessary, because you explicitly import all of the itertools methods that you use. The two from itertools import
statements can be combined into one.
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
This is a reasonable way to check a single number for primality if the number isn't too large, but since you're checking lots of numbers and you mention this as a performance concern, I would suggest that you think about building a sieve of Eratosphenes for smallish numbers (up to say 10 million or 100 million) and using probabilistic primality testing for numbers larger than that. Perhaps BPSW.
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
The name suggests a search, but it's actually an evaluation. I'd call it something like evalPoly(coeffs, x)
. The evaluation can be made more efficient using Horner's method, which can be written as a reduce
call.
It would be worth adding a docstring to document the order of the coefficients: constant term first (a_0, ..., a_n
) or last (a_n, ..., a_0
).
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
Why is this duplicating the contents of findSingleValue
rather than calling it?
if not isPrime(polyValue):
return False
return True
Why not use all(...)
?
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
I'd half expect permtools
to have a built-in method for this. Alternatively it can be done with (untested code) itertools.product(range(LB, HB+1), deg)
.
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
This could use a refactor: one method to do the work, and then the main method just does the I/O.
allCombosNum = (abs(HB - LB))**deg - 1
I think this has an out-by-one error.
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
My suggestion above would make this unnecessary, but... tuple(repeat(LB, deg))
?
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
I think this is polyList = [prime] + list(combo)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
There's a potential performance improvement here. If lowDegPoly == "n"
then it's more efficient to avoid generating and testing polynomials of lower degree.
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
primeList = [findSingleValue(item, x) for x in range(prime - 1)]
verifyPrime()
verifyPrime()
That recursive call is rather inelegant, and the direct invocation of the main method is not considered best practice. It would be better to replace these lines with
if __name__ == "__main__":
while True:
verifyPrime()
$endgroup$
Firstly, on documentation: the standard term for the object you're searching for is prime-generating polynomial. "Prime polynomial" is often used as a synonym for "irreducible polynomial", and while there is a relationship between reducibility and generation of primes it's best to use standard terms in documentation where possible.
from math import sqrt; from itertools import count, islice
import itertools
from itertools import product
This looks a bit untidy. It's not very Pythonic to put multiple statements on a line separated by ;
. The import itertools
is unnecessary, because you explicitly import all of the itertools methods that you use. The two from itertools import
statements can be combined into one.
#is n prime?
def isPrime(n):
#https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
This is a reasonable way to check a single number for primality if the number isn't too large, but since you're checking lots of numbers and you mention this as a performance concern, I would suggest that you think about building a sieve of Eratosphenes for smallish numbers (up to say 10 million or 100 million) and using probabilistic primality testing for numbers larger than that. Perhaps BPSW.
#find P(x) using the polyList to represent the polynomial
def findSingleValue(polyList, x):
#https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
return sum((a*x**i for i,a in enumerate(polyList)))
The name suggests a search, but it's actually an evaluation. I'd call it something like evalPoly(coeffs, x)
. The evaluation can be made more efficient using Horner's method, which can be written as a reduce
call.
It would be worth adding a docstring to document the order of the coefficients: constant term first (a_0, ..., a_n
) or last (a_n, ..., a_0
).
#is the polynomial prime for x <= p - 1?
def isPolyPrime(polyList, prime):
#polyValue = 0
for x in range(prime - 1):
polyValue = sum((a*x**i for i,a in enumerate(polyList)))
Why is this duplicating the contents of findSingleValue
rather than calling it?
if not isPrime(polyValue):
return False
return True
Why not use all(...)
?
#generate the next combo, given the previous combo
def genCombo(combo, LB, HB):
deg = len(combo)
combo = list(combo)
index = deg - 1
while index >= 0:
if combo[index] < HB:
combo[index] += 1
index = -1
elif combo[index] == HB:
combo[index] = LB
index -= 1
combo = tuple(combo)
return combo
I'd half expect permtools
to have a built-in method for this. Alternatively it can be done with (untested code) itertools.product(range(LB, HB+1), deg)
.
#main function
def verifyPrime():
prime = int(input("Enter the prime number you want to find a poly for: "))
LB = int(input("Enter the lower bound: "))
HB = int(input("Enter the higher bound: "))
deg = int(input("Enter the degree of the polynomial: "))
lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
This could use a refactor: one method to do the work, and then the main method just does the I/O.
allCombosNum = (abs(HB - LB))**deg - 1
I think this has an out-by-one error.
combo = ()
#create the first combo - this is used as the basis to generate more combos
for x in range(deg):
combo += (LB,)
My suggestion above would make this unnecessary, but... tuple(repeat(LB, deg))
?
polyList =
polyList.append(prime)
for coef in combo:
polyList.append(coef)
I think this is polyList = [prime] + list(combo)
#now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
isGoodPoly = isPolyPrime(polyList, prime)
if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
goodPolyList.append(polyList)
There's a potential performance improvement here. If lowDegPoly == "n"
then it's more efficient to avoid generating and testing polynomials of lower degree.
primeList =
for x in range(prime - 1):
primeList.append(findSingleValue(item, x))
primeList = [findSingleValue(item, x) for x in range(prime - 1)]
verifyPrime()
verifyPrime()
That recursive call is rather inelegant, and the direct invocation of the main method is not considered best practice. It would be better to replace these lines with
if __name__ == "__main__":
while True:
verifyPrime()
answered Dec 12 '18 at 9:07
Peter TaylorPeter Taylor
16.2k2860
16.2k2860
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209498%2ffind-prime-polynomials-for-a-users-given-prime-bounds-and-degree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
How does P(x) = 11 - 2x + 2x^3 satisfy the P(0) = 0 requirement?
$endgroup$
– 200_success
Dec 12 '18 at 14:46
$begingroup$
Sorry, I suppose I wasn't very clear on that. -2x + 2x^3 is P(x) and 11 is the prime p.
$endgroup$
– Gizmo
Dec 12 '18 at 22:42