¿Cómo puedo imprimir los números primos que terminan en 1 en un rango determinado y el más cercano después de b?

luz:
  • de entrada de la muestra: 10 100

  • Resultado de muestra: 11,31,41,61,71,101

  • en el código anterior que puedo conseguir el valor de salida de la muestra hasta el valor 71, ¿cómo puedo conseguir más próximo número primo después de que termina con 1 b.

Aquí está el código Traté:

import java.util.*;
public class Program{
public static void main(String[] args){
    Scanner in=new Scanner(System.in);
    int a=in.nextInt();
    int b=in.nextInt();
    int i,j,count;
    for(i=a;i<=b;i++)
    {
        for(j=2;j<=b;j++)
        {
            if(i%j==0)
            break;
        }
        if(j==i && j%10==1) 
        {
            System.out.println(i);
        }
    }
} 
Arvind Kumar Avinash:
  1. Es necesario tener una variable para almacenar el último primer modo que se puede comprobar si es igual al límite superior
  2. No es necesario dividir un número por los números hasta que el fin de comprobar si es primo. Sólo tiene que comprobar hasta su raíz cuadrada. Compruebe https://en.wikipedia.org/wiki/Primality_test

Hacerlo de la siguiente manera:

import java.util.Scanner;

public class Program {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter two integers separated by space: ");
        int a = in.nextInt();
        int b = in.nextInt();
        int i, j, sqrt, lastPrime = 0;
        for (i = a; i <= b; i++) {
            sqrt = (int) Math.sqrt(i);
            for (j = 2; j <= sqrt; j++) {
                if (i % j == 0) {
                    break;
                }
            }
            // If the loop with j completed without a break, the number is prime. Note that
            // 1 is not a prime number.Also, the last digit of the number should be 1.
            if (j > sqrt && Math.abs(i) != 1 && i % 10 == 1) {
                System.out.print(i + " "); // Print the prime
                lastPrime = i;// Store the prime to lastPrime
            }
        }

        // If the last prime is not equal to the upper limit, find the next prime
        if (b != lastPrime) {
            boolean nextPrimeFound = false;
            do {
                sqrt = (int) Math.sqrt(i);
                for (j = 2; j <= sqrt; j++) {
                    if (i % j == 0) {
                        break;
                    }
                }
                if (j > sqrt && Math.abs(i) != 1 && i % 10 == 1) {
                    nextPrimeFound = true;
                    System.out.println(i);
                }
                i++;
            } while (!nextPrimeFound);
        }
    }
}

Un análisis de la muestra:

Enter two integers separated by space: 10 100
11 31 41 61 71 101

Otra muestra de la ejecución:

Enter two integers separated by space: 10 200
11 31 41 61 71 101 131 151 181 191 211

Con el fin de hacer que el programa modular, se puede poner el código de repetición en una función de la siguiente manera:

import java.util.Scanner;

public class Program {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("Enter two integers separated by space: ");
        int a = in.nextInt();
        int b = in.nextInt();
        int i, j, sqrt, lastPrime = 0;
        for (i = a; i <= b; i++) {
            if (isPrimeEndingWith1(i)) {
                System.out.print(i + " "); // Print the prime
                lastPrime = i;// Store the prime to lastPrime
            }
        }

        // If the last prime is not equal to the upper limit, find the next prime
        if (b != lastPrime) {
            boolean nextPrimeFound = false;
            do {
                if (isPrimeEndingWith1(i)) {
                    nextPrimeFound = true;
                    System.out.println(i);
                }
                i++;
            } while (!nextPrimeFound);
        }
    }

    static boolean isPrimeEndingWith1(int n) {
        if (Math.abs(n) == 1 || n == 0) {
            return false;
        }
        int sqrt = (int) Math.sqrt(n), j;
        for (j = 2; j <= sqrt; j++) {
            if (n % j == 0) {
                break;
            }
        }
        // If the loop with j completed without a break, the number is prime. Note that
        // 1 is not a prime number.Also, the last digit of the number should be 1.
        if (j > sqrt && Math.abs(n) != 1 && n % 10 == 1) {
            return true;
        }
        return false;
    }
}

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=409027&siteId=1
Recomendado
Clasificación