美文网首页
【python练习题】验证哥德巴赫猜想

【python练习题】验证哥德巴赫猜想

作者: 好男儿2019 | 来源:发表于2019-04-02 22:35 被阅读0次

编制判断素数的Sub函数或者Function函数,验证哥德巴赫猜想:一个不小于6的偶数可以表示为两个素数之和。例如,6=3+3,8=5+3,10=3+7.

【解答】

x=int(input("请输入一个大于或者等于6的偶数:"))

def isPrime(val):

    number=val

    while number>=6:

        if number%2==0:#判断是否偶数

            #print(str(number)+"是偶数")

            a=number  #除数

            b=a #被除数

            c=a%b #余数

            while b>0:

                #余数去掉偶数,余数是0和9的。

                if  c%2==0 or c==0 or c==9 or (b%5==0 and b>5) or 2*b<a or (b%3==0 and b>3)or (b%7==0 and b>7):

                    b=b-1

                    if b==0:

                        break

                    c=a%b

                    continue

                print(str(a)+"="+str(b)+"+"+str(c)) #除数=被除数+余数

                b=b-1

                if b==0:

                    break

                c=a%b

            number=number-1 #输入数递减

        else:

            #print(str(number)+"不是偶数")

            number=number-1

            continue

    print("输入的值已经小于6")

isPrime(x)

相关文章

网友评论

      本文标题:【python练习题】验证哥德巴赫猜想

      本文链接:https://www.haomeiwen.com/subject/bftobqtx.html