大输入错误答案

时间:2018-10-10 17:26:22

标签: python math

我正在尝试确定是否可以将a> b表示为整数,其中b> 1且给定的整数大于0。我的代码对于除一个以外的所有测试用例都运行良好。 对于以下输入,它给出错误的答案: 536870912 我不明白为什么。

library(ggplot2)

ggplot(sleep, 
       aes(x=ID,fill=group,y=extra))+
  geom_point(
    size=3,
    pch = 21,
    position = position_dodge2(width=c(0.7,
                                       rep(0.00001,3),
                                       0.2,
                                       rep(0.00001,5),
                                       0.00001,
                                       rep(0.00001,3),
                                       0.2,
                                       rep(0.00001,5)))
  ) + 
  scale_y_continuous(limits = c(-2,6)) +
  theme_classic() + 
  theme(panel.grid.major.x = element_line(color="gray",
                                          linetype = "dashed"))

1 个答案:

答案 0 :(得分:0)

这将解决:

from  math import log
def isPower(A):
    if(A==1):
        return (1)
    if (A&-A)==(A):
        return (1)        
    for i in range(2,int(A**0.5)+1):
        val=log(A,i)
        if(int(val)-val==0):
            return (1)
    else:
        return (0)

如果您要查找其不起作用的原因,请参阅以下链接:

Dealing with accuracy in Python math operations