from math import sqrt
def fermatBook (n):
x=int(sqrt(n))
c=x**2-n
while (sqrt(c)!=int(sqrt(c))):
x=x+1
y=sqrt(c)
a=x+y
b=x-y
if a==1 or b==1:
print "The number is prime"
return a, b
错误:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
fermatBook (23867)
File "C:/Python27/fermatLivro.py", line 6, in fermatBook
while (sqrt(c)!=int(sqrt(c))):
ValueError: math domain error
我不知道程序出了什么问题。。。有人能帮我吗?你知道吗
很可能变量c为负:
示例
如果您致电:
它将为以下变量分配以下值:
对于平方根不是整数的
n
的值,很可能会发生这种情况。你知道吗然后,当调用
sqrt(c)
时,它超出了域,因为它不能处理负值。你知道吗你应该使用一些可以处理虚数的东西,例如^{}
或进行检查以确保不会发生这种情况:
举个例子。。。你知道吗
但是,一旦你解决了这个问题,你就会遇到另一个问题:
这是一个无限循环:
您需要更新c,否则无论您增加
x
多少次,条件都不会改变。你可能是这个意思?你知道吗相关问题 更多 >
编程相关推荐