import java.util.Scanner;publicclassPrime{publicstaticvoidmain(String[] args){/* 求素数等一系列问题 */// 1.从2到x-1测试是否可以整除
Scanner in =newScanner(System.in);int x = in.nextInt();boolean isPrime =true;if(x ==1)
isPrime =false;for(int i =2; i < x ; i++){if(x % i ==0){
isPrime =false;break;}}if(isPrime)
System.out.println(x+" is prime.");else
System.out.println(x+" is not prime.");}}
import java.util.Scanner;publicclassPrime{publicstaticvoidmain(String[] args){/* 求素数等一系列问题 */// 2.去掉偶数后,从3到x-1,每次+2.
Scanner in =newScanner(System.in);int x = in.nextInt();boolean isPrime =true;if(x ==1|| x %2==0&& x !=2)
isPrime =false;else{for(int i =3; i < x ; i+=2){if(x % i ==0){
isPrime =false;break;}}}if(isPrime)
System.out.println(x+" is prime.");else
System.out.println(x+" is not prime.");}}
import java.util.Scanner;publicclassPrime{publicstaticvoidmain(String[] args){/* 求素数等一系列问题 */// 3.去掉偶数后,从3到sqrt(x),每次+2.
Scanner in =newScanner(System.in);int x = in.nextInt();boolean isPrime =true;if(x ==1|| x %2==0&& x !=2)
isPrime =false;else{for(int i =3; i < Math.sqrt(x); i+=2){if(x % i ==0){
isPrime =false;break;}}}if(isPrime)
System.out.println(x+" is prime.");else
System.out.println(x+" is not prime.");}}
import java.util.Scanner;publicclassPrime{publicstaticvoidmain(String[] args){/* 求素数等一系列问题 */// 4.判断是否能被已知的且<x的素数整除,构造前50个素数的表。
Scanner in =newScanner(System.in);int[] primes =newint[50];
primes[0]=2;int cnt =1;
MAIN_LOOP:for(int x =3; cnt < primes.length; x++){for(int i =0; i < cnt; i++){if(x % primes[i]==0){continue MAIN_LOOP;}}
primes[cnt++]= x;}for(int k : primes){
System.out.print(k+" ");}}}
import java.util.Scanner;publicclassPrime{publicstaticvoidmain(String[] args){/* 求素数等一系列问题 */// 5.构造素数表:令x为2,将2x、3x、4x直至ax<n的数标记为非素数,令x为下一个没有被标记为非素数的数。
Scanner in =newScanner(System.in);boolean[] isPrime =newboolean[100];for(int i =2; i < isPrime.length; i++){
isPrime[i]=true;}for(int i =2; i < isPrime.length; i++){if(isPrime[i]){for(int k =2; i * k < isPrime.length; k++){
isPrime[i*k]=false;}}}for(int i =0; i < isPrime.length; i++){if(isPrime[i])
System.out.print(i+" ");}}}