Maple: If a number is an integer/square

625 Views Asked by At

I'm writing a Maple procedure and I have a line that is "if ... then ..." and I would like it to be if k is an integer (or if k^2 is a square) - how would onw say that in Maple? Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

To check whether something is an integer, you can use the type command. For example,

> type( 2/3, integer );     
             false

> type( 3, integer );       
              true

To check whether an integer is a perfect square, you can use the issqr command, as in the following examples:

> issqr(16);
              true

> issqr(15); 
             false

Thus, your if statement might (if I'm guessing your intent correctly) look something like this.

if type( k, integer ) and issqr( k ) then
     DoSomething()
end if
0
On

There is a shorthand for type checking in an if statement:

if k::integer then
     # do something
end if

To me, that is more elegant.