Python 如何有效地枚举所有完美正方形?
我制作了以下程序,要求用户输入上界,并计算和打印上界的每个完美正方形。但是,我认为我的Python 如何有效地枚举所有完美正方形?,python,performance,math,Python,Performance,Math,我制作了以下程序,要求用户输入上界,并计算和打印上界的每个完美正方形。但是,我认为我的is_perfect_square函数不是很有效,因为当上界为数千或更多时,计算完美平方需要很长时间。我想知道如何使我的程序更高效,我认为使用数学模块来使用sqrt可以工作,但我不是数学家,所以请求帮助。 我的计划是: """Print all the perfect squares from zero up to a given maximum.""" import math def read_bound(
is_perfect_square
函数不是很有效,因为当上界为数千或更多时,计算完美平方需要很长时间。我想知道如何使我的程序更高效,我认为使用数学模块来使用sqrt
可以工作,但我不是数学家,所以请求帮助。
我的计划是:
"""Print all the perfect squares from zero up to a given maximum."""
import math
def read_bound():
"""Reads the upper bound from the standard input (keyboard).
If the user enters something that is not a positive integer
the function issues an error message and retries
repeatedly"""
upper_bound = None
while upper_bound is None:
line = input("Enter the upper bound: ")
if line.isnumeric() and int(line) >= 0:
upper_bound = int(line)
return upper_bound
else:
print("You must enter a positive number.")
def is_perfect_square(num):
"""Return true if and only if num is a perfect square"""
for candidate in range(1, num):
if candidate * candidate == num:
return True
def print_squares(upper_bound, squares):
"""Print a given list of all the squares up to a given upper bound"""
print("The perfect squares up to {} are: ". format(upper_bound))
for square in squares:
print(square, end=' ')
def main():
"""Calling the functions"""
upper_bound = read_bound()
squares = []
for num in range(2, upper_bound + 1):
if is_perfect_square(num):
squares.append(num)
print_squares(upper_bound, squares)
main()
您可以使用
sqrt
import math
def is_perfect_square(num):
"""Return true if and only if num is a perfect square"""
root = math.sqrt(num)
return int(root) - root == 0
或者如@PacoH所示:
return root.is_integer()
您可以使用
sqrt
import math
def is_perfect_square(num):
"""Return true if and only if num is a perfect square"""
root = math.sqrt(num)
return int(root) - root == 0
或者如@PacoH所示:
return root.is_integer()
正如您使用
math.sqrt
所说的:
import math
def is_perfect_square(num):
"""Return true if and only if num is a perfect square"""
return math.sqrt(num).is_integer()
正如您使用
math.sqrt
所说的:
import math
def is_perfect_square(num):
"""Return true if and only if num is a perfect square"""
return math.sqrt(num).is_integer()
我会完全颠倒逻辑,首先取上界的平方根,然后打印每个正整数的平方小于或等于该数字:
upper_bound = int(input('Enter the upper bound: '))
upper_square_root = int(upper_bound**(1/2))
print([i**2 for i in range (1, upper_square_root+1)])
绑定78的输出示例:
[1,4,9,16,25,36,49,64]
这样可以避免许多不必要的循环和数学计算 我会完全颠倒逻辑,首先取上限的平方根,然后打印每个正整数的平方小于或等于该数字:
upper_bound = int(input('Enter the upper bound: '))
upper_square_root = int(upper_bound**(1/2))
print([i**2 for i in range (1, upper_square_root+1)])
绑定78的输出示例:
[1,4,9,16,25,36,49,64]
这样可以避免许多不必要的循环和数学计算 平方是奇数的部分和:
1 = 1
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
因此,您可以简单地执行以下操作:
square = 1
odd = 1
while square <= upper_bound:
print(square)
odd = odd + 2
square = square + odd
square=1
奇数=1
而平方平方是奇数的部分和:
1 = 1
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
因此,您可以简单地执行以下操作:
square = 1
odd = 1
while square <= upper_bound:
print(square)
odd = odd + 2
square = square + odd
square=1
奇数=1
而平方这只会将运行时间减少到O(上限)
而O(sqrt(上限))
是可以实现的。这只会将运行时间减少到O(上限)
而O(sqrt(上限))
是可以实现的。