Skip to main content
4 of 5
added 6 characters in body

How can I reduce the time to run this code?

I am trying to find the integers $ a, b, c, d \in [-15, -1] \cup [1, 15] $ so that the equation $ \left| x^2 + a x + b \right| = c x + d $ has four distinct integeral solutions different from $ 0 $.

To this end, I tried

ClearAll[a, b, c, d];
    sol = x /. Solve[{Abs[x^2 + a x + b] == c x + d} , x, Reals];
    ClearAll[f];
    (f[{a_, b_, c_, d_}] := 
        Quiet@Check[And @@ (IntegerQ /@ #), False]) &[sol]
    poss = Select[
       Tuples[Range[-15, 
         15], {4}],  #[[1]] #[[2]] #[[3]] #[[4]] != 
          0  && #[[1]]^2 - 4 #[[2]] > 0 && #[[2]]^2 - #[[4]]^2 != 0 && 
         f[#] &];
    Take[poss, Length[poss]];
    With[{s = sol}, 
     getSolution[poss_] := Block[{a, b, c, d}, {a, b, c, d} = poss;
       Join[poss, s]]]
    getSolution /@ poss

and got 426 solutions. But the time is about 15 minutes. How can I reduce the time?

minhthien_2016
  • 4.7k
  • 1
  • 14
  • 31