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]
#给定第一个五位数,后续数字与第一个数的差值
def is_prime(n):
#判断是否是质数,这里没有考虑1,因为本题不需要
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之间的数是否存在质数
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
#三个两位数的成绩,规定三个数从小到大分别是n1, n2, n3。由于使用数字不能重复,所以有上述的最小和最大值
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的两个数字重复,则在set中只会记录一个数,这样的情况被自动排除
for n2 in range(n1 + 1, max_n2 + 1):
used2 = used1.copy()
#在n1的基础上每次寻找能n2都重新创建一个set,避免指针错误造成程序的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
#因为要寻找一个由平方数组成的三位数,所以最大平方数可以计算,限定这个上限是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)
#创建一个字典,key是一个三位数,value是构成他的两个平方数的tuple,因为题中可以寻找任意平方数的组合,所以字典中只保存一个,新出现的覆盖前面的内容
three_digit = set()
for i in l.keys():
three_digit.add(int(i))
three_digit = sorted(list(three_digit))
#按照从小到大顺序创建一个可以由两个平方数求和表示的三位数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))
#如果连续三个三位数都可以表征为两个数的平方和,则加入到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)