#8945: (JAVA)思考一下


johnnyjong823 (johnny)


寫JAVA的可以朝6N+1和6N+5的方向去思考喔!

一開始先排出掉2、3、5的倍數,就會可以在時間內了

但JAVA好像很難到一秒內。

請高手再釋出他的方式 

#8948: Re:(JAVA)思考一下


must (must)


寫JAVA的可以朝6N+1和6N+5的方向去思考喔!

一開始先排出掉2、3、5的倍數,就會可以在時間內了

但JAVA好像很難到一秒內。

請高手再釋出他的方式 


很驚險過的

import java.util.*;

import java.math.*;

public class JAVA{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

while(sc.hasNextInt()) {

BigInteger input=new BigInteger(sc.next());

System.out.println(input.isProbablePrime(1)? "質數":"非質數");

} //end while

} //end main

} //end JAVA classd 

#8949: Re:(JAVA)思考一下


must (must)


寫JAVA的可以朝6N+1和6N+5的方向去思考喔!

一開始先排出掉2、3、5的倍數,就會可以在時間內了

但JAVA好像很難到一秒內。

請高手再釋出他的方式 


很驚險過的

import java.util.*;

import java.math.*;

public class JAVA{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

while(sc.hasNextInt()) {

BigInteger input=new BigInteger(sc.next());

System.out.println(input.isProbablePrime(1)? "質數":"非質數");

} //end while

} //end main

} //end JAVA classd