Java — prime number

Definition: Prime numbers are also called prime numbers. A natural number greater than 1, except 1 and itself, a number that cannot be divisible by other natural numbers is called a prime number; otherwise it is called a composite number (it is specified that 1 is neither a prime number nor a composite number)
code:

package 练习;
import  java.util.Scanner;
public  class Text {
    
    
    public  static  int  fn(int n) {
    
    
        int j,k;
        k=(int)Math.sqrt(n);
        for(j=2;j<=k;j++) {
    
    
            if(n%j==0) {
    
    
                return 0;
            }
        }
        return 1;
    }
    public static void main(String[] args) {
    
    
        int  i,m=0,t;
        Scanner ming =new Scanner(System.in);
        t=ming.nextInt();
        for(i=2;i<=t;i++)
        {
    
    
            if(fn(i)==1) {
    
    
                System.out.print(i+" ");
                m++;
                if(m%10==0){
    
    
                    System.out.println();
                    m=0;
                }
            }
        }

    }
}

10
2 3 5 7

Guess you like

Origin blog.csdn.net/qq_44859533/article/details/108808948