试编写Python程序,根据哥德巴赫猜想一:任何大于6的偶数都可表示为两个素数之和,对100000内的偶数进行验证

如题所述

#coding=utf-8
import sys,time
from math import sqrt

def isprime(num):
    for i in range(2,int(sqrt(num))+1):
        if num%i==0:
            return False
    return True
    
def gedebahe(num):
    for i in range(2,num//2+1):
        if isprime(i) and isprime(num-i):
            return True
    return Falsese

def main(n):
    for i in range(6,n+1,2):
        if not gedebahe(i,L):
            print('fail at number {}'.format(i))
            break
    else:
        tip='success: all evens in {} can be the sum of two primes.'
        print(tip.format(n))

if __name__=='__main__':
    start=time.time()
    n=100000
    if len(sys.argv)>1:
        n=int(sys.argv[1])
    main(n)
    end=time.time()
    print('cost {} seconds'.format(end-start))

[willie@bogon pys]$ python gedebahe.py 

success: all evens in 100000 can be the sum of two primes.

cost 24.8260421753 seconds

#coding=utf-8
import sys,time
from math import sqrt

def primes(n):
    # prepair data space
    plist = [0, 0] + list(range(2,n+1))    
    for i in range(2, int(sqrt(n))+1):
        if plist[i]:
            plist[i+i::i] = [0] * len(plist[i+i::i])
    return filter(None, plist)
    
def gedebahe(n,L):    
    for i in L:
        if (n-i) in L:
            return True
        elif i>n//2+1:
            return False

def main(n):
    L=list(primes(n))
    for i in range(6,n+1,2):
        if not gedebahe(i,L):
            print('fail at number {}'.format(i))
            break
    else:
        tip='success: all evens in {} can be the sum of two primes.'
        print(tip.format(n))

if __name__=='__main__':
    start=time.time()
    n=100000
    if len(sys.argv)>1:
        n=int(sys.argv[1])
    main(n)
    end=time.time()
    print('cost {} seconds'.format(end-start))

[willie@bogon pys]$ python gedebahe.py 

success: all evens in 100000 can be the sum of two primes.

cost 70.5533659458 seconds

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-05
# -*- coding: utf-8 -*-
"""
Created on Thu Apr  5 01:14:35 2018

@author: pandeer
# Any question if you have ,pls contact my WeChat:yangpan4997
"""
list = []

def isPrise(n):
    #验证输入的数值是否符合要求,即:是否为大于6的偶数,否则重新输入
    while n <= 6 or (n % 2 != 0 and n > 6):
        n = int(input("输入错误,请重新输入,输入值必须大于6的偶数:"))
        
    #输出所有的n以内的质数,并将所有的质数存储于list[]列表中等待处理
    for j in range(2,n+1):
               
            for i in range(2, j):
                 if j % i == 0:
                      
                      break
                      
            else:
                print(j,'是一个质数')
                list.append(j)
    # 输出所有的质数列表,仅做最终表的列出,故做判定语句处理
            if j == n - 1:      
               print(list)    
           
    #对列表中的质数进行进行计算,若存在两个质数的和等于输入的偶数n,则输出这两个质数
    #为避免出现A+B与B+A(质数相加)的等价情况,增加list[i] > list[j]语句进行过滤
               for i in range(len(list)) :
                     for j in range(len(list)) :
                       if list[i] + list[j] == n and list[i] > list[j]:
                            print ("偶数%d等于两个质数的和:"%n,list[i],'+',list[j])
                      
n = isPrise(int(input("请输入大于等于6的偶数:")))

相似回答