#usr/bin/env python3
import time
from math import sqrt
start=time.time()
n=100000
isprime=lambda p: all([p%d for d in range(2,int(sqrt(p))+1)])
for i in range(6,n+1,2):
for j in range(2,i//2+1):
if isprime(j) and isprime(i-j):
break
else:
print('fail at {}'.format(i))
break
print('\r{:.2f}%'.format(i/n*100),end='')
else:
print('\nsuccess: all evens in {} is the sum of two primes.'.format(n))
end=time.time()
print('consum time: {:.3f} seconds'.format(end-start))
[willie@bogon pys]$ python3 gede.py
100.00%
success: all evens in 100000 is the sum of two primes
consum time: 16.739 seconds
#usr/bin/env python3
import sys
import time
from math import sqrt
from threading import Thread
isprime=lambda p: all([p%d for d in range(2,int(sqrt(p))+1)])
def sum_of_two_evens(num):
for i in range(2,num//2+1):
if isprime(i) and isprime(num-i):
return True
return False
interrupt = False
def check(a,b,per):
global interrupt
for i in range(a,b+1,2):
if not sum_of_two_evens(i) or interrupt:
interrupt = True
break
if 1:
print('\r{:.2f}%'.format((i-a)/(b-a)*100),end='')
def main(n=100000):
start=time.time()
thread_count=10
threads=[]
a=6
b=n//thread_count
for i in range(thread_count):
t=Thread(target=check,args=(a,b,i*thread_count))
t.start()
t.join()
a,b=b,n//thread_count*(i+2)
for t in threads:
t.join()
if interrupt:
print('fail at number {}'.format(i))
else:
tip = 'Yes: all evens>6 in {} can be the sum of the primes.'.format(n)
print('\n{}\n{} threads is used in this case.'.format(tip,thread_count))
end=time.time()
print('consum time: {:.3f} seconds'.format(end-start))
if __name__ == '__main__':
if len(sys.argv)>1:
main(int(sys.argv[1]))
else:
main()
[willie@bogon pys]$ python3 gedethreads.py
100.00%
Yes: all evens>6 in 100000 can be the sum of the primes.
10 threads is used in this case.
consum time: 16.486 seconds
#usr/bin/env python3
import sys
import time
import multiprocessing
from math import sqrt
from multiprocessing import Process
def isprime(p):
'''check whether is a prime
>>> isprime(2),isprime(9),isprime(12)
(True, False, False)
>>> isprime(5),isprime(121),isprime(113)
(True, False, True)
'''
return all([p%d for d in range(2,int(sqrt(p))+1)])
def sum_of_two_evens(num):
'''check whether a num can be the sum of two evens
>>> sum_of_two_evens(8)
True
>>> sum_of_two_evens(11)
False
'''
for i in range(2,num//2+1):
if isprime(i) and isprime(num-i):
return True
return False
tasks=[]
def saysuccess(n,task_count):
tip = 'success: all evens>6 in {} can be the sum of the primes.'.format(n)
print('\n{}\n{} processes is used in this case.'.format(tip,task_count))
def sayfail(i):
print('\nfail at number {}'.format(i))
def check(a,b,p,start,c,n):
'''check whether evens between a and b are all sum_of_two_evens'''
global interrupt
for i in range(a,b+1,2):
if not sum_of_two_evens(i):
sayfail(i)
[t.terminate() for t in tasks]
break
if p==c-1:
print('\r{:.2f}%'.format((i-a)/(b-a)*100),end='')
else:
if p==c-1:
saysuccess(n,c)
print('consum time: {:.3f}'.format(time.time()-start))
if __name__ == '__main__':
start=time.time()
import doctest
doctest.testmod(verbose=True)
n=100000
if len(sys.argv)>1:
n=int(sys.argv[1])
task_count=10
a=6
b=n//task_count
for i in range(task_count):
t=Process(target=check,args=(a,b,i,start,task_count,n))
a,b=b,n//task_count*(i+2)
t.start()
tasks.append(t)
[willie@bogon pys]$ python3 gedeprocess.py
Trying:
isprime(2),isprime(9),isprime(12)
Expecting:
(True, False, False)
ok
Trying:
isprime(5),isprime(121),isprime(113)
Expecting:
(True, False, True)
ok
Trying:
sum_of_two_evens(8)
Expecting:
True
ok
Trying:
sum_of_two_evens(11)
Expecting:
False
ok
4 items had no tests:
__main__
__main__.check
__main__.sayfail
__main__.saysuccess
2 items passed all tests:
2 tests in __main__.isprime
2 tests in __main__.sum_of_two_evens
4 tests in 6 items.
4 passed and 0 failed.
Test passed.
100.00%
success: all evens>6 in 100000 can be the sum of the primes.
10 processes is used in this case.
consum time: 8.480