如何在Python中测试费马最后定理的案例

时间:2015-07-21 02:58:14

标签: python

练习5.3。费马的最后定理说没有正整数a,b和c这样

a ^ n + b ^ n = c ^ n

任何大于2的n值。

  1. 编写一个名为check_fermat的函数,它接受四个参数-a,b,c和n-并检查Fermat定理是否成立。如果n大于2且结果为真
  2. a ^ n + b ^ n = c ^ n

    程序应该打印出来,“圣烟,费马错了!”否则程序应该打印出来,“不,那不行。”

    1. 编写一个提示用户输入a,b,c和n值的函数,将它们转换为整数,并使用check_fermat检查它们是否违反了费马定理。

1 个答案:

答案 0 :(得分:1)

def check_fermat(a, b, c, n):
    if n > 2 and (a**n + b**n == c**n):
        print("Holy smokes, Fermat was wrong!")
    else:
        print("No, that doesn’t work.")

def check_numbers():
    a = int(input("Choose a number for a: "))
    b = int(input("Choose a number for b: "))
    c = int(input("Choose a number for c: "))
    n = int(input("Choose a number for n: "))
    return check_fermat(a, b, c, n)

check_numbers()

可能总会返回“不,那不行”....