def is_prime(n): for k in range(2, n):#1 if n % k == 0: return False#2 else: continue #3 return True #4 print(is_prime(12)) #5 print(is_prime(13)) #5→
import datetime as dt dt1 = dt.datetime nt1 = dt1.now() #1 prime_list = [] #2 def is_prime(n): for k in range(2, n): if n % k == 0: return False else: continue return True for x in range(2, 1000): if is_prime(x) == True: prime_list.append(x) #3 nt2 = dt1.now() #4 print(prime_list, '\n', nt2-nt1) #5→
def is_prime(n): for k in range(3, n, 2): if n % k == 0: return False else: continue return True
import datetime as dt import math as mt dt1 = dt.datetime nt1 = dt1.now() prime_list = [2] def is_prime(n): sq = int(mt.sqrt(n)) for k in range(3, sq+1, 2): #1 if n % k == 0: return False else: continue return True for x in range(3, 1000, 2): if is_prime(x) == True: prime_list.append(x) nt2 = dt1.now() print(prime_list, '\n', nt2-nt1)→ これにより,所要時間は約0.001秒と10分の1ほどに減る.
prime_list = [2,3] def is_prime(n): for k in prime_list[1:]: #3以後で割る if n % k == 0: return False else: continue return True
import datetime as dt dt1 = dt.datetime nt1 = dt1.now() prime_list = [2,3] def is_prime(n): for k in prime_list[1:]: #3以後で割る if n % k == 0: return False else: continue return True def generate_prime(max1): p = 3 while p < max1: p += 2 if is_prime(p) == True: prime_list.append(p) generate_prime(1000) nt2 = dt1.now() print(prime_list,'\n',nt2-nt1)→ 所要時間は約0.003秒とむしろ遅くなる.
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() file1 = open('c:/data/test1.txt','wt') file1.write('2,3') def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def generate_prime3(max1): p = 3 while p < max1: p += 2 if is_prime(p) == True: file1.write(','+str(p)) generate_prime3(1000)#1 file1.close() tn2 = dt1.now() print(tn2-tn1)→ ファイル書き込みを行うのでやや遅くなるが,所要時間は約0.004秒になる.
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() prime_list = '' def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def generate_prime(max1): global prime_list p = 3 per10000 = 0 kosuu = 2 while p < max1: p += 2 if is_prime(p) == True: kosuu += 1 if int(p/10000) > per10000: prime_list += str(kosuu) +'\t'+str(p)+'\n' per10000 = int(p/10000) generate_prime(1001000) print(prime_list) tn2 = dt1.now() print(tn2-tn1)→ 100組の値を比較すると,次のグラフのようになる.きれいに一致するわけではないが,近づくという感じは分かる.
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() prime_list = [2,3,5,7,11,13,17,19,23,29,31,37, 41,43,47,53,59,61]#,67,71,73,79,83,89,97] ok_list =[] okN_list = [] no_list =[] noN_list = [] def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return n else: continue return True def product_plus1(): global prime_list for n in range(len(prime_list)): N = 1 k = 0 for k in range(n+1): N *= prime_list[k] N += 1 if is_prime(N) == True: ok_list.append(prime_list[n]) okN_list.append(N) else: no_list.append(prime_list[n]) noN_list.append(str(N)+':' +str(is_prime(N))) product_plus1() print(ok_list,'\n',okN_list,'\n',no_list,noN_list) tn2 = dt1.now() print(tn2-tn1)→
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() p_list = [2,] d_list = [] d_count =[[1,1],] def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def make_prime(max1): global p_list for m in range(3, max1+1, 2): if is_prime(m) == True: p_list.append(m) make_prime(10000)#1 for n in range(len(p_list)-1): d_list.append(p_list[n+1]-p_list[n]) for x in range(2,max(d_list)+1,2): count1 = d_list.count(x) d_count.append([x, count1]) print(d_count) tn2 = dt1.now() print(tn2-tn1)→
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def n_to_2n(n): for x in range(n+1, 2*n+1): if is_prime(x) == True: return x return -x for n in range(1, 100000):#1 if n_to_2n(n) < 0: print(n) tn2 = dt1.now() print(tn2-tn1)→ 所要時間約3.9で100000まで成り立つことが示される.#1の引数を書き換えると範囲を変えられる.
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def sq_to_next(n): for x in range(n**2+1, (n+1)**2):#1 if is_prime(x) == True: return x return 0 for n in range(1, 10000):#2 if sq_to_next(n) == 0: print(n) tn2 = dt1.now() print(tn2-tn1)→ 約7.4秒で,n=10000,すなわちn2が1億の場合まで成り立つことが言える.#2の箇所でn=1000すなわちn2が100万の場合を調べるには,約0.087秒でできる.
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() prime_list = [2,]#1 count_dic = dict() def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def generate_prime(max1): global prime_list for n in range(3, max1, 2): if is_prime(n) == True: prime_list.append(n) def is_gold(n): L = len(prime_list) for p in range(L): if prime_list[p] < n: continue N = p for m in range(N, 0, -1): for k in range(1, L): if (prime_list[m]+prime_list[k]) > n: break if (prime_list[m]+prime_list[k]) == n: return prime_list[k] else: continue return False num1 = 20000 ### generate_prime(num1) for n in range(6, num1, 2): k = is_gold(n) if k == False: print(n) else: value1 = count_dic.get(k, None) if value1 == None: count_dic[k] = 1 else: count_dic[k] +=1 count_list = sorted(list(count_dic)) count_list2 = list(map(lambda x:(x,count_dic[x]), count_list)) print(count_list2) tn2 = dt1.now() print(tn2-tn1)→
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def twin_prime(max1): p6 = int(max1/6) for k in range(p6, p6+1000): x = 6*k-1 if (is_prime(x) == True) \ and (is_prime(x+2) == True): print(x, x+2) break twin_prime(10000000000000) #1 tn2 = dt1.now() print(tn2-tn1)→ 10000000001267 10000000001269
import math as mt import datetime as dt dt1 = dt.datetime tn1 = dt1.now() def is_prime(p1): sq = int(mt.sqrt(p1)) for n in range(3, sq+1,2): if p1 % n == 0: return False else: continue return True def inv_plus(max1): sum1 = 1/2 for x in range(3, max1, 2): if is_prime(x) == True: sum1 += 1/x if x % int(max1/10) == 1: print(x,'\t',sum1) max1 = 5000000 ### inv_plus(max1)→