# How do I filter only integer solutions in sagemath?

How do I filter only integer solutions in sagemath?

import time
Start_Time = time.time()
var('x y N M F f G g L l Q q S s U u')

eq0 = N-1019 == 0

eq3 = (32*((2*(3*3/4*M+1)-3*(-x)+1)/24)+3*(2*f-1)^2-3)/8-F==0
eq4 = ((2*(3*3/4*M+1)-3*(-x)+1)/24)+3/4*M- F == 0
eq5 = (32*((2*(3*F+1)-3*(f)+1)/24)+3*(2*g-1)^2-3)/8-G==0
eq6 = ((2*(3*F+1)-3*(f)+1)/24)+3/4*M- G == 0
eq7 = (32*((2*(3*G+1)-3*(g)+1)/24)+3*(2*l-1)^2-3)/8-L==0
eq8 = ((2*(3*G+1)-3*(g)+1)/24)+3/4*M- L == 0
eq9 = (32*((2*(3*L+1)-3*(l)+1)/24)+3*(2*q-1)^2-3)/8-Q==0
eq10 = ((2*(3*L+1)-3*(l)+1)/24)+3/4*M- Q == 0
eq11 = (32*((2*(3*Q+1)-3*(q)+1)/24)+3*(2*s-1)^2-3)/8-S==0
eq12 = ((2*(3*Q+1)-3*(q)+1)/24)+3/4*M- S == 0
eq13 = (32*((2*(3*S+1)-3*(s)+1)/24)+3*(2*u-1)^2-3)/8-U==0
eq14 = ((2*(3*S+1)-3*(s)+1)/24)+3/4*M- U == 0

eq15 =u+1==0

eq16 = (N-3)/8+y*(y-1)/2-M == 0

eq17 = 2*x*(x+1)-M == 0

solutions = solve([eq0,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13,eq14,eq15,eq16,eq17],x,y,N,M,F,f,G,g,L,l,Q,q,S,s,U,u)
sol = solutions
Execution_Time = time.time() - Start_Time
print (Execution_Time)
print(sol)

edit retag close merge delete

Sort by » oldest newest most voted

In Python, you can filter a list using list comprehension. Among the 128 solutions found, only 2 are integer solutions:

sage: len(sol)
128
sage: def is_integer_solution(solution):
....:     return all(eq.rhs() in ZZ for eq in solution)
....:
sage: integer_solutions = [s for s in sol if is_integer_solution(s)]
sage: len(integer_solutions)
2
sage: integer_solutions
[[x == 127,
y == -254,
N == 1019,
M == 32512,
F == 30496,
f == -63,
G == 32016,
g == -31,
L == 32392,
l == -15,
Q == 32484,
q == -7,
S == 32506,
s == -3,
U == 32511,
u == -1],
[x == 127,
y == 255,
N == 1019,
M == 32512,
F == 30496,
f == -63,
G == 32016,
g == -31,
L == 32392,
l == -15,
Q == 32484,
q == -7,
S == 32506,
s == -3,
U == 32511,
u == -1]]

more