Como posso imprimir os números primos que terminam com 1 em um determinado intervalo e o mais próximo depois de b?

Light:
  • entrada de amostra: 10 100

  • Exemplo de saída: 11,31,41,61,71,101

  • a partir do código acima eu posso obter o valor de saída da amostra até o valor de 71, como posso obter mais próximo número primo terminando com 1 depois b.

Aqui está o código que eu tentei:

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. Você precisa ter uma variável para armazenar o último privilegiada para que você possa verificar se ele é igual ao limite superior
  2. Você não precisa dividir um número por números até que, a fim de verificar se ele é primo. Você só precisa verificar se a sua raiz quadrada. Verifique https://en.wikipedia.org/wiki/Primality_test

Fazê-lo da seguinte forma:

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);
        }
    }
}

Uma corrida de amostra:

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

Outra corrida de amostra:

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

A fim de tornar a modular programa, você pode colocar o código repetindo em uma função da seguinte forma:

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;
    }
}

Acho que você gosta

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