Python - Check if a number is a square

孤街浪徒 提交于 2021-02-08 04:36:38

问题


I wrote a function that returns whether a number input is a square or not

def is_square(n):
    if n<1:
        return False
    else:
        for i in range(int(n/2)+1):
            if (i*i)==n:
                return True
            else:
                return False

I am confident that this code works. But when I did the test cases, example:test.expect( is_square( 4)), it says that the value is not what was expected.


回答1:


Your function doesn't actually work, as it will immediatley return False on the first non-square-root found. Instead you will want to modify your code to be:

def is_square(n):
    if n<1:
        return False
    else:
        for i in range(int(n/2)+1):
            if (i*i)==n:
                return True
        return False

such that it only returns false once all possible square roots have been checked. You may also want to look into math.sqrt() and float.is_integer(). Using these methods your function would become this:

from math import sqrt

def is_square(n):
    return sqrt(n).is_integer()

Keep in mind that this method will not work with very large numbers, but your method will be very slow with them, so you will have to choose which to use. Hope I helped!




回答2:


To stick to integer-based algorithms, you might look at implementation of binary search for finding square root:

def is_square(n):
    if n < 0:
        return False
    if n == 0:
        return True
    x, y = 1, n
    while x + 1 < y:
        mid = (x+y)//2
        if mid**2 < n:
            x = mid
        else:
            y = mid
    return n == x**2 or n == (x+1)**2



回答3:


The main idea of Python philosophy is to write simple code. To check if a number is an perfect square:

def is_square(n):
    return n**0.5 == int(n**0.5)

When power to a float you can find the root of a number.




回答4:


def myfunct(num):
    for i in range(0,num):
        if i*i==num:
            return 'square'
    else:
        return 'not square'



回答5:


Easiest working solution, but not for large numbers.

def squaretest(num):
    sqlist=[]
    i=1
    while i**2 <= num:
        sqlist.append(i**2) 
        i+=1
    return num in sqlist



回答6:


Assuming that n >= 0:

def is_square(n):
    tmp = int(n ** 0.5)
    return n == tmp * tmp
          
print(is_square(81), is_square(67108864 ** 2 + 1))  # True False



回答7:


Another approach:

    def SQRT(x):
        import math
        if math.sqrt(x) == round(math.sqrt(x)):
            return True
        else:
            return False



回答8:


You can simply use simpy module import it as,

from sympy.ntheory.primetest import is_square 

and you can check for a number like this:

is_square(number) 

It will return a boolean value




回答9:


def is_square(n):
if n< 0:
    return False
return round(n ** 0.5) ** 2 == n



回答10:


To check if a number is a square, you could use code like this:

import math
number = 16
if math.sqrt(number).is_interger:
        print "Square"
else:
        print "Not square"

import math imports the math module.

if math.sqrt(number).is_interger: checks to see if the square root of number is a whole number. If so, it will print Square. Otherwise, it will print Not square.




回答11:


Since math.sqrt() returns a float, we can cast that to a string, split it from "." and check if the part on the right(the decimal) is 0.

import math
def is_square(n):
x= str(math.sqrt(n)).split(".")[1]  #getting the floating part as a string.
return True if(x=="0") else False


来源:https://stackoverflow.com/questions/44531084/python-check-if-a-number-is-a-square

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