1. Finding particular sequences of prime numbers
Write a program consecutive_primes.py that finds all sequences of 6 consecutive prime 5-digit numbers, say (a,b,c,d,e,f), with b = a+2, c = b+4, d = c+6, e = d+8, and f = e+10. So a, b, c, d and e are all 5-digit prime numbers and no number between a and b, between b and c, between c and d, between d and e, and between e and f is prime.
from math import sqrt
difference = [0, 2, 6, 12, 20, 30]
#給定第一個(gè)五位數(shù)斋竞,后續(xù)數(shù)字與第一個(gè)數(shù)的差值
def is_prime(n):
#判斷是否是質(zhì)數(shù)院究,這里沒有考慮1锋华,因?yàn)楸绢}不需要
if n % 2 == 0:
return False
for i in range(3, round(sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
solution = []
for i in range(10000, 100000):
candidate = [0] * 6
flag = 0
for j in range(len(difference)):
candidate[j] = difference[j] + i
if not is_prime(candidate[j]):
break
if is_prime(candidate[-1]):
for e in range(candidate[0], candidate[5] + 1, 2):
#判斷一組candidate之間的數(shù)是否存在質(zhì)數(shù)
if is_prime(e) and e not in candidate:
flag = 1
if not flag:
solution.append(candidate)
for i in solution:
for e in i:
print(e, end = ' ')
print()
2. Finding particular sequences of triples
Write a program triples_1.py that finds all triples of positive integers (i, j, k) such that i, j and k are two digit numbers, no digit occurs more than once in i, j and k, and the set of digits that occur in i, j or k is equal to the set of digits that occur in the product of i, j and k.
min_n1 = 10
max_n1 = 76
max_n2 = 87
max_n3 = 98
#三個(gè)兩位數(shù)的成績(jī),規(guī)定三個(gè)數(shù)從小到大分別是n1, n2, n3利虫。由于使用數(shù)字不能重復(fù),所以有上述的最小和最大值
for n1 in range(min_n1, max_n1 + 1):
used1 = set()
used1.add(n1 // 10)
used1.add(n1 % 10)
if len(used1) != 2:
continue
#如果n1的兩個(gè)數(shù)字重復(fù),則在set中只會(huì)記錄一個(gè)數(shù)取视,這樣的情況被自動(dòng)排除
for n2 in range(n1 + 1, max_n2 + 1):
used2 = used1.copy()
#在n1的基礎(chǔ)上每次尋找能n2都重新創(chuàng)建一個(gè)set,避免指針錯(cuò)誤造成程序的bug
used2.add(n2 // 10)
used2.add(n2 % 10)
if len(used2) != 4:
continue
for n3 in range(n2 + 1, max_n3 + 1):
used3 = used2.copy()
used3.add(n3 // 10)
used3.add(n3 % 10)
if len(used3) != 6:
continue
multiple = n1 * n2 * n3
flag = 0
if len(str(multiple)) != 6:
flag = 1
for i in str(multiple):
if int(i) not in used3:
flag = 1
else:
used3.remove(int(i))
if not flag:
print('%d * %d * %d = %d' % (n1, n2, n3, multiple))
3. Finding special triples of the form (n, n + 1, n + 2)
Write a program triples_2.py that finds all triples of consecutive positive three-digit integers each of which is the sum of two squares, that is, all triples of the form (n, n + 1, n + 2) such that:
--n, n+1 and n+2 are integers at least equal to 100 and at most equal to 999;
--eachofn,n+1andn+2isoftheforma2+b2.
from math import sqrt
from collections import defaultdict
up = round(sqrt(999)) + 1
#因?yàn)橐獙ふ乙粋€(gè)由平方數(shù)組成的三位數(shù)常挚,所以最大平方數(shù)可以計(jì)算作谭,限定這個(gè)上限是up
l = defaultdict(list)
for i in range(0, up):
for j in range(i + 1, up):
total = i ** 2 + j ** 2
if total < 1000 and total > 99:
l[total] = (i, j)
#創(chuàng)建一個(gè)字典,key是一個(gè)三位數(shù)奄毡,value是構(gòu)成他的兩個(gè)平方數(shù)的tuple折欠,因?yàn)轭}中可以尋找任意平方數(shù)的組合,所以字典中只保存一個(gè),新出現(xiàn)的覆蓋前面的內(nèi)容
three_digit = set()
for i in l.keys():
three_digit.add(int(i))
three_digit = sorted(list(three_digit))
#按照從小到大順序創(chuàng)建一個(gè)可以由兩個(gè)平方數(shù)求和表示的三位數(shù)list
result = []
for i in three_digit:
if (i + 1) in three_digit and (i + 2) in three_digit:
result.append((i, i + 1, i + 2))
#如果連續(xù)三個(gè)三位數(shù)都可以表征為兩個(gè)數(shù)的平方和锐秦,則加入到list中
while result:
print(result[0],'(equal to (%d^2+%d^2, %d^2+%d^2, %d^2+%d^2)) is a solution.' % \
(l[result[0][0]][0], l[result[0][0]][1], l[result[0][1]][0], l[result[0][1]][1], l[result[0][2]][0], l[result[0][2]][1]))
result.pop(0)