我正在尝试创建一个脚本,该脚本将自动循环显示一组数字,并对照Fermat反例检查它们。我已经有一个脚本,可以从用户那里输入数字并对照反例检查它们。我想做的是使该系统自动化,并在特定范围内获取数字,然后在反例a n + b n = c ** n中检查这些数字的所有可能组合n> 2。我当时正在考虑绘制方程式,并让其在该图中运行。如果您对此有任何其他想法,请提供帮助。我已经检查了其他一些资源,但无法真正掌握如何将图形添加到代码中。
def prompt_yesno(prompt):
return input(prompt) in 'Yy'
def prompt_int(prompt, min=None):
while True:
try:
ans = int(input(prompt))
if min is None or min <= ans:
return ans
except ValueError:
print("Input must be an integer.")
def is_fermat_counterexample(a, b, c, n):
return n > 2 and a**n + b**n == c**n
def fermat_demo():
print("Let's see if Fermat was right.")
print("He claims that a^n + b^n = c^n cannot be true for any n > 2.")
a = prompt_int('Give a positive integer for "a": ', 1)
b = prompt_int('Give a positive integer for "b": ', 1)
c = prompt_int('Give a positive integer for "c": ', 1)
n = prompt_int('Give an integer bigger than 2 for exponent "n": ', 3)
print("Fermat was incorrect"
if is_fermat_counterexample(a, b, c, n) else
"No, that does not work!")
while True:
fermat_demo()
if not prompt_yesno("Would you like to try again?\n"
"Type 'Y' to continue and 'N' to exit: "):
break
答案 0 :(得分:0)
最好仅取a,b和n的范围并计算c。 如果a,b的范围是[1到X],n的范围是[3到N],则对(a,b,n)的每个组合调用以下方法以找到一个反例-
def is_fermat_counterexample(a, b, n):
value = a**n + b**n
c = value**(1/n)
return (c.is_integer() and (int(c) > 0))