The title
In 2004, Terence Tao and Ben Green proved that for any large n, there exists an arithmetic sequence consisting of n terms that are all prime numbers. For example {7,37,67,97,127,157} is the solution to n=6. For a given n, find a set of maximal solutions within a specified range.
Input format: Input two positive integers in a line: n (≤10) is the number of items in an arithmetic prime sequence; MAXP (2≤MAXP<10 5) is the upper bound of the largest prime number in the sequence.
Output format: If the solution exists, output the set of solutions with the greatest difference in a row in increasing order; If the solution is not unique, the set of solutions with the largest number of heads is output. If no solution exists, the output does not exceed the maximum prime of MAXP. The digits in each line are separated by one space. There must be no extra space at the beginning and end of each line.
Example 1:5 1000 No blank line at the end Example 1:23 263 503 743 983 No blank line at the end Example 2:10 200 No blank line at the end Example 2:199 No blank line at the end Example 1:23 263 503 743 983 Example 2:10 200 No blank line at the end Example 2:199 No blank line at the endCopy the code
Their thinking
n, MAXP = map(int,input().split()) # n, MAXP = map(int,"5 1000".split()) # n, MAXP = map(int,"10 2".split()) import math def isSushu(input:int)->bool: if input == 2 or input == 3 or input == 1: return True sqrtInt = int(math.sqrt(input)) for i in range(2,sqrtInt+1): if input%i == 0: return False return True sushuList = [] for i in range(2,MAXP+1): if isSushu(i) == True: sushuList.append(i) # print(sushuList) if len(sushuList) == 0: print("") resList = [] for index,val in enumerate(sushuList): for i in sushuList[index+1:]: Chengshu in range(1,n): dengcha = I - val shifoucunzai = true dengchashu = val + dengcha*(chengshu) if dengchashu not in sushuList: shifoucunzai = False break if shifoucunzai == True: resList.append((val, Sort (key= lambda x:(-x[1],-x[0])) # print(resList) if len(resList)>0: Then output the group of solutions with the greatest difference in ascending order in a row; If the solution is not unique, the set of solutions with the largest number of heads is output. num,cha = resList[0] res = [str(num+cha*i) for i in range(n)] print(" ".join(res)) else: try: print(str(sushuList[-1])) except: print("")Copy the code