If you want to send it in python, just send it

Problem Description

        1898——If you want to send it, send it. Please arrange all prime numbers up to 1993 in the first row from smallest to largest. Each number on the second row is equal to the difference between the two adjacent prime numbers above it. Programming to find out: Are there several consecutive integers in the second row of numbers whose sum is exactly 1898? If it exists, how many such situations are there?

        We assume that there is a continuous list of prime numbers P that exactly meets the requirements of the above problem, then let's analyze this problem. The first line is P[0], P[1],···,P[n], the second line is P[1] - P[0], P[2] - P[1],···,P [n] - P[n-1]. Add the numbers in the second row to P[n] - P[0], so P[n] - P[0] = 1898. The derivation process is shown in the figure below:

So the essence of this question is whether there are two prime numbers whose difference is equal to 1898 among the prime numbers less than or equal to 1993.

If you want to send it, send it

        Since you want to ask, the essence of the problem is to find out whether there are two prime numbers whose difference is equal to 1898 among the prime numbers less than or equal to 1993. Then we can first make a generator for generating prime numbers to list all prime numbers less than or equal to 1993. Then look in this prime number list to see if there are two prime numbers whose difference is equal to 1898, and if so, output them.

prime number generator

        First make a prime number generator to help us generate prime numbers. We use the Ehrlich sieve method here to generate prime numbers. The code is as follows:

def primes_generator(n):
    """
    素数生成器

    :param n: 生成的最后一个素数小于等于n
    :return: Generator
    """
    yield 2  # 素数生成器遇到第一个next函数时返回2,并等待下一个next函数
    primes = list(range(3, n + 1, 2))  # 创建第一个元素为3最后一个元素小于n+1的奇数列表
    while primes:  # primes为空列表时结束循环
        yield primes[0]  # 素数生成器遇到next函数时返回列表primes的第一个元素,并等待下一个next函数
        primes = [i for i in primes if i % primes[0] > 0]  # 使用列表生成器把列表primes中的每一个元素都对primes的第一个元素取余,如果余数大于0则在新列表中添加该元素,最后返回新列表赋值给primes

If you want to send, send the execution function

        We have finished the prime number generator, and we can use the prime number generator to quickly generate the prime numbers we need. Now you only need to design a program to find out whether there are two prime numbers whose difference is 1898 among the prime numbers not exceeding 1993. We first use the largest prime number as the minuend and try to subtract from the first prime number 2, and then try backward one by one to see which prime number has a difference less than or equal to 1898. If the difference is equal to 1898, it means that a set of solutions has been found. If the difference is less than 1898, there is no need to try backwards. Just use the second largest prime number as the minuend to try to find the solution. This cycle continues until the solution is found. When subtracting a prime number that is less than 1898, there is no need to look for it (if you subtract a number that is already less than 1898, it will be even smaller). code show as below:

def primes_sum_1898(big, primes_sum):
    """
    要发就发问题

    :param big: 不大于某个数
    :param primes_sum: 连续素数之和
    :return:
    """
    primes_list = list(primes_generator(big))  # 使用list函数把素数生成器中的生成的素数全部取出来变成一个素数列表
    for i in primes_list[::-1]:  # 逆序输出素数列表
        if i < primes_sum:  # 判断素数i是否小于连续素数之和
            break  # i小于连续素数之和时结束循环
        for n in primes_list:  # 正序输出素数列表
            if i - n < primes_sum:  # 判断两个素数之差是否小于连续素数之和
                break  # 两个素数之差小于连续素数之和时结束循环
            elif i - n == primes_sum:  # 判断两个素数之差是否等于连续素数之和
                print(primes_list[primes_list.index(n):primes_list.index(i)+1])  # 打印出满足条件的连续素数列表

Complete code

        Combining a prime number generator with a hit-and-miss execution function, we get a program that solves the hit-and-miss problem. The complete code is as follows:

def primes_generator(n: int):
    """
    素数生成器

    :param n: 生成的最后一个素数小于等于n
    :return: Generator
    """
    yield 2  # 素数生成器遇到第一个next函数时返回2,并等待下一个next函数
    primes = list(range(3, n + 1, 2))  # 创建第一个元素为3最后一个元素小于n+1的奇数列表
    while primes:  # primes为空列表时结束循环
        yield primes[0]  # 素数生成器遇到next函数时返回列表primes的第一个元素,并等待下一个next函数
        primes = [i for i in primes if i % primes[0] > 0]  # 使用列表生成器把列表primes中的每一个元素都对primes的第一个元素取余,如果余数大于0则在新列表中添加该元素,最后返回新列表赋值给primes


def primes_sum_1898(big: int, primes_sum: int):
    """
    要发就发问题

    :param big: 不大于某个数
    :param primes_sum: 连续素数之和
    :return:
    """
    primes_list = list(primes_generator(big))  # 使用list函数把素数生成器中的生成的素数全部取出来变成一个素数列表
    for i in primes_list[::-1]:  # 逆序输出素数列表
        if i < primes_sum:  # 判断素数i是否小于连续素数之和
            break  # i小于连续素数之和时结束循环
        for n in primes_list:  # 正序输出素数列表
            if i - n < primes_sum:  # 判断两个素数之差是否小于连续素数之和
                break  # 两个素数之差小于连续素数之和时结束循环
            elif i - n == primes_sum:  # 判断两个素数之差是否等于连续素数之和
                print(primes_list[primes_list.index(n):primes_list.index(i)+1])  # 打印出满足条件的连续素数列表


primes_sum_1898(1993, 1898)

The execution results are as follows:

We can see that there are 3 sets of solutions to the problem. The first set is continuous prime numbers from 89 to 1987, the second set is continuous prime numbers from 53 to 1951, and the third set is continuous prime numbers from 3 to 1901. With the above program, we can easily solve similar problems when we want to send them.

Guess you like

Origin blog.csdn.net/qq_40148262/article/details/130992581