 <p>Quick answer:</p> <p>First af all you are starting from x=0 and then increasing it which is not the best solution since you are looking for the maximum value and not the first one. So for that I would go from an upperbound that can be</p> <p>x=abs((b)^(1/4))</p> <p>than decrease from that value, and as soon you find an element &lt;=b you are done. </p> <p>You can even think in this way:</p> <pre><code>for y=b to 1 solve(x^4+x^3+x^2+x+1=y) if has an integer solution then return solution </code></pre> <p>See <a href="https://en.wikipedia.org/wiki/Quartic_function#Solving_a_quartic_equation" rel="nofollow">this</a></p> <p>This is a super quick answer I hope I didn't write too many stupid things, and sorry I don't know yet how to write math here.</p>