python 寻找完美的正方形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1547196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding perfect square
提问by eozzy
I have this python code:
我有这个python代码:
def sqrt(x):
ans = 0
if x >= 0:
while ans*ans < x:
ans = ans + 1
if ans*ans != x:
print x, 'is not a perfect square.'
return None
else:
print x, ' is a perfect square.'
return ans
else:
print x, ' is not a positive number.'
return None
y = 16
sqrt(y)
the output is:
输出是:
16 is not a perfect square.
Whereas this works perfectly:
而这完美地工作:
x = 16
ans = 0
if x >= 0:
while ans*ans < x:
ans = ans + 1
#print 'ans =', ans
if ans*ans != x:
print x, 'is not a perfect square'
else: print ans, 'is a perfect square'
else: print x, 'is not a positive number'
What am I doing wrong?
我究竟做错了什么?
回答by Sunjay Varma
Just thought I'd contribute a simpler solution:
只是想我会提供一个更简单的解决方案:
def is_square(n):
return sqrt(n).is_integer()
This is valid for n < 2**52 + 2**27 = 4503599761588224
.
这对n < 2**52 + 2**27 = 4503599761588224
.
Examples:
例子:
>>> is_square(4)
True
>>> is_square(123)
False
>>> is_square(123123123432**2)
True
回答by CMS
Indent your code correctly to let the while
statement execute until ans*ans < x
:
正确缩进代码以让while
语句执行直到ans*ans < x
:
def sqrt(x):
ans = 0
if x >= 0:
while ans*ans < x:
ans = ans + 1
if ans*ans != x: # this if statement was nested inside the while
print x, 'is not a perfect square.'
return None
else:
print x, ' is a perfect square.'
return ans
else:
print x, ' is not a positive number.'
return None
y = 16
print sqrt(y)
Try it out here.
回答by Greg Hewgill
Your while
loop only executes once. No matter which branch the if
statement inside it takes, the whole function will return immediately.
你的while
循环只执行一次。不管里面的if
语句在哪个分支,整个函数都会立即返回。
回答by pavium
Change your code so it displays the value of ans
as well as x
, so you can tell how many times the loop is executed.
更改您的代码,以便它显示的值ans
,以及x
,所以你可以告诉多少次的循环执行。
回答by ManicMailman
EDITI modified it, tried it out, and it works. You just need this piece of code
编辑我修改了它,试了一下,它的工作原理。你只需要这段代码
As soon as ans = 4, ans * ans is no longer smaller than x. Try while ans*ans <= x: instead of just <
只要 ans = 4,ans * ans 就不再小于 x。尝试 while ans*ans <= x: 而不仅仅是 <
def sqrt(x):
ans = 0
if x >= 0:
while ans*ans <= x:
if ans*ans == x:
print x, ' is a perfect square.'
return ans
else:
ans = ans + 1
回答by stiank81
If your code sample is actually correctly indentet the first round of the while will return on it's first round - always. So any positive value of x>1 will fullfil the ans*ans=1*1=1!=x, giving "x is not a perfect square".
如果您的代码示例实际上是正确缩进的,while 的第一轮将在第一轮返回 - 始终。所以 x>1 的任何正值都将满足 ans*ans=1*1=1!=x,给出“x 不是一个完美的平方”。
You basically needs to get your indentation right - like you do in your other example. Again - if your code sample here actually is correctly indented. Try this:
您基本上需要正确缩进 - 就像您在其他示例中所做的那样。再次 - 如果您的代码示例实际上已正确缩进。试试这个:
def sqrt(x):
ans = 0
if x >= 0:
while ans*ans < x:
ans = ans + 1
if ans*ans != x:
print x, 'is not a perfect square.'
return None
else:
print x, ' is a perfect square.'
return ans
else:
print x, ' is not a positive number.'
return None
回答by trycatch22
def isPerfectSquare(number):
return len(str(math.sqrt(number)).split('.')[1]) == 1
回答by Suman Michael
I think this is probably short.
我想这可能很短。
def issquare():
return (m**.5 - int(m**.5)==0)
回答by gumption
If the goal is to determine whether a number is a perfect square, I would think it would be simpler (and perhaps more efficient) to use math builtins, e.g.:
如果目标是确定一个数字是否是一个完美的平方,我认为使用数学内置函数会更简单(也许更有效),例如:
def is_perfect_square(n):
if not ( ( isinstance(n, int) or isinstance(n, long) ) and ( n >= 0 ) ):
return False
else:
return math.sqrt(n) == math.trunc(math.sqrt(n))