Search code examples
prologclpfd

Clpfd not a square number


Is it possible to make a constraint for an integer to say it can not be a (Perfect) square number?

I have:

square(Square):- N#>0, Square #= N*N.

How do I define notsquare(Notsquare):- ...

My first thought was to have P*P =Q*Q*Notsquare and Remainder #>0, Remainder #= P rem Q.

But P and Q need to be able to be non integers so this didn't work.


Solution

  • What about

    notSquare(S):- N #> 0, R #>0, R #< 2*N+1, S #= N*N+R.
    

    ?

    Should work if S > 0; if you need to work with negative numbers too, I suppose you could modify it as

    notSquare(S):- S #> 0, N #> 0, R #>0, R #< 2*N+1, S #= N*N+R.
    notSquare(S):- S #< 0, SM #= -S, notSquare(SM).