NO_Goldbach's other conjecture

题目:

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.

9 = 7 + 2×12
15 = 7 + 2×22
21 = 3 + 2×32
25 = 7 + 2×32
27 = 19 + 2×22
33 = 31 + 2×12

It turns out that the conjecture was false.

What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

代码为:
__author__ = 'Administrator'
#coding=utf-8
import math,time,itertools,functools
prime_list=[2]
judge_dict={1:False,2:True}
def cal_time(fn):
    @functools.wraps(fn)
    def wraper(*args,**kwargs):
        start_time=time.time()
        fn(*args,**kwargs)
        end_time=time.time()
        print "time:",end_time-start_time
    return wraper

def IsPrimer(intnum):
    #判断一个数是否为质数
    if intnum in judge_dict:
        return judge_dict[intnum]
    else:
        judge_value=True
        if not intnum%2:
            judge_value=False
        else:
            for divisor in xrange(3,int(math.sqrt(intnum)+1),2):
                if not intnum%divisor:
                    judge_value=False
                    break
        judge_dict[intnum]=judge_value
        if judge_value==True:
            prime_list.append(intnum)
        return judge_value
def IsOddComposite(int_num):
    if not  IsPrimer(int_num) and int_num%2:
        return True
    return False
def IsMeetCondition(num):
    "判断一个奇合数是否满足条件(奇合数需要另外判断)"
    re_int=0#0表示非奇合数,1表示奇合数,2表示能被写成素数与平方的2倍之和的数,3表示不能

    if not IsOddComposite(num):
        re_int=0
    else:
        re_int=1
        meet_ondition=False
        for tmp_prime in  itertools.dropwhile(lambda  x:x>=num,prime_list[:]):
            root=math.sqrt((num-tmp_prime)/2)
            if int(root)==root:
                meet_ondition=True
                break
        if meet_ondition==False:
            re_int=3
        else:
            re_int=2
    return re_int
@cal_time
def mainFunc():
    start_num=3
    while 1:
        if IsMeetCondition(start_num)==3:
            print start_num
            break
        else:
            start_num+=2
if __name__=='__main__':
    mainFunc()
    """
    start_num=3
    while 1:
        if IsMeetCondition(start_num)==3:
            print start_num
            break
        else:
            start_num+=2

    start_num=5777
    print IsMeetCondition(start_num)
    for tmp_prime in  itertools.dropwhile(lambda  x:x>=start_num,prime_list[:]):

            root=math.sqrt((start_num-tmp_prime)/2)
            print start_num,tmp_prime,root
            if int(root)==root:
                meet_ondition=True
                break
    """



运行结果为:
5777
time: 0.0829999446869
别人的代码为:
import math,functools,time
def timer(func):
    @functools.wraps(func)
    def wraper_func(*args,**kwargs):
        start_time=time.time()
        func(*args,**kwargs)
        end_time=time.time()
        print "time:",end_time-start_time
    return wraper_func
def eratosthenesSieve(limit):
    sieve = [True for i in range(limit)]
    root = int(limit ** 0.5) + 1
    primeNumbers = [2, 3, 5]
    for i in range(2, root):
        if sieve[i]:
            for j in range((i * i), limit, i):
                sieve[j] = False
    for k in range(7, limit, 2):
        if sieve[k]:
            primeNumbers.append(k)
    return primeNumbers

def isTwiceSquare(number):
    n = math.sqrt(number / 2)
    if int(n) == n:
        return True
    else:
        return False

@timer
def main():
    done = False
    primeNumbers = eratosthenesSieve(10000)
    n = 33
    while not done:
        if n not in primeNumbers:
            i = 0
            while n > primeNumbers[i]:
                if isTwiceSquare(n - primeNumbers[i]):
                    done = False
                    break;
                else:
                    done = True
                i = i + 1
        else:
            pass
        n = n + 2
    print('result : ', n - 2)

main()

运行结果为:

('result : ', 5777)
time: 0.154000043869


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值