PAT B 1013 (JAVA) - Long brush brother's title path

Primes 1013 (20 minutes)
so that P i denotes the i-th prime number. All primes current to two positive integers M≤N≤10 ^ 4, to make the PN output P M.
Input format:
input given M and N in a row, separated by a space therebetween.
Output format:
output P M from the PN to all prime numbers, each representing a digit line 10, separated by spaces therebetween, the end of the line may not have extra space.

Sample input:
527
Output Sample:
1,113,171,923,293,137 43 is 41 is
4,753,596,167,717,379 83 89
97 101 103

From 2 start counting until it reaches N, all primes required output according to casual working

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        int m=in.nextInt();
        int n=in.nextInt();
        int j=1,k=0;
        for(int i=2;k<n;i++){
            if(isPrime(i)){
                k++;
                if(k>=m){
                    if(j%10==0){
                        System.out.println(" "+i);
                    }else if(j%10==1){
                        System.out.print(i);
                    }else{
                        System.out.print(" "+i);
                    }
                    j++;
                }
            }
        }
    }
    public static boolean isPrime(int x){
        if(x<=3)
            return true;
        for(int i=2;i<=(int)Math.sqrt(x);i++){
            if(x%i==0){
                return false;
            }
        }
        return true;
    }
}

Published 13 original articles · won praise 0 · Views 246

Guess you like

Origin blog.csdn.net/qq_23079139/article/details/104082779