题目:
5777
time: 0.0829999446869
别人的代码为:
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 + 212
15 = 7 + 222
21 = 3 + 232
25 = 7 + 232
27 = 19 + 222
33 = 31 + 212
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