How to check if a number is an integer in Pari/GP?

僤鯓⒐⒋嵵緔 提交于 2020-01-17 06:18:12

问题


I'm trying to write an if statement like this

 if(denominator([(i-1)! + 1] / i)-1,print(hi),print(ho))

i can be any integer for example 10, when I set i to 10 it gives this error.

? [(x-1)! + 1] / x
  ***   this should be an integer: [(x-1)!+1]/x
                                    ^-----------

I really only need to check if [(x-1)! + 1] / x is an integer or not the denominator thing is what I came up with, I also tried Mod but couldn't get that working either.


回答1:


It seems that you are confused with the names x and i. Please, see that expression below works properly:

i = 10;
print([(i-1)! + 1] / i);
gp > [362881/10]



回答2:


I'm not sure what the error was but I ended up using a floor function for determining if it was an integer or not.




回答3:


You could use:

print(if(((i-1)! + 1) % i, "hi", "ho"))

If i (in your question x) is not an integer, you get an error from the ! (factorial) operator (but see gamma as well).

Do not use [] here, it creates a vector.

The opreator % which I used, gives the remainder. For example 11 % 4 gives the integer 3. In comparison Mod(11, 4) is not an ordinary integer, it is a member of the ring Z/4Z (integers modulo 4). That is very useful in many cases.

I supposed you wanted to write out strings, so I used quotes ". If hi and ho are variables, omit the quotes of course.



来源:https://stackoverflow.com/questions/36783151/how-to-check-if-a-number-is-an-integer-in-pari-gp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!