【Alignment prime sequence】

arithmetic prime sequence

To search for an arithmetic prime number sequence with a length of 10, you can enumerate the tolerance d from small to large, and then enumerate the first item p from small to large, and check whether p, p+d, p+2d, ..., p+9d are all prime numbers .

import java.util.*;

public class PrimeArithmetic {
    
    
    // 判断是否为素数
    public static boolean isPrime(int n) {
    
    
        if (n < 2) {
    
    
            return false;
        }
        int sqrt_n = (int) Math.sqrt(n);
        for (int i = 2; i <= sqrt_n; i++) {
    
    
            if (n % i == 0) {
    
    
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
    
    
        int length = 10;
        int max_d = 1000; // 公差最大值
        for (int d = 1; d <= max_d; d++) {
    
    
            for (int p = 2; p <= 10000; p++) {
    
    
                boolean is_arithmetic = true;
                for (int i = 0; i < length; i++) {
    
    
                    int num = p + i * d;
                    if (!isPrime(num)) {
    
    
                        is_arithmetic = false;
                        break;
                    }
                }
                if (is_arithmetic) {
    
    
                    System.out.println("公差为 " + d + ",首项为 " + p);
                    return;
                }
            }
        }
        System.out.println("未找到长度为 " + length + " 的等差素数列。");
    }
}

The tolerance of an arithmetic prime sequence must be relatively prime to the leading term.

Explanation: Assume that the first item of the arithmetic prime sequence is a, the tolerance is d, and a and d have a common factor p greater than 1. Then, the second term in this arithmetic prime sequence is a+d, which is also divisible by p. Therefore, the second term in this arithmetic prime sequence is not a prime number. Therefore, if the tolerance of an arithmetic prime sequence is not coprime with the first term, then the arithmetic prime sequence cannot be all prime numbers.

Guess you like

Origin blog.csdn.net/m0_60641871/article/details/129391302