Lanqiao Cup official website fill-in-the-blank questions (prime numbers)

Question description

This question is a fill-in-the-blank question. You only need to calculate the result and use the output statement in the code to output the filled-in result.

We know that the first prime number is 2, the second prime number is 3, the third prime number is 5…

Could you please calculate what the 2019th prime number is?

operating restrictions

  • Maximum running time: 1s
  • Maximum running memory: 128M
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
      int ans=1;
        for(int i=3;;i++){
          if(check(i)==1){
            ans++;
          }
          if(ans==2019){
            System.out.println(i);
            break;
          }
        }
    }
    public static int check(int n){
      int flag=1;
      for(int i=2;i<n;i++){
        if(n%i==0){
          flag=0;
          break;
        }
      }
      return flag;
    }
}

Guess you like

Origin blog.csdn.net/s44Sc21/article/details/132851341