题解 | #判断素数个数#
判断素数个数
http://www.nowcoder.com/practice/c6ad83181a17408eb7605d51a251dd9a
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int start = scanner.nextInt(); int end = scanner.nextInt(); method(start,end); } public static void method(int start, int end) { int count=0; if(start>end) { int temp=start; start=end; end=temp; } //write your code here...... for(int i=start;i<=end;i++) { if(i>2&&fun(i)) { count++; } } System.out.println(start+"到"+end+"之间有"+count+"个大于2的素数"); } public static boolean fun(int num) { for(int i=2;i<=Math.sqrt(num);i++) { if(num%i==0) { return false; } } return true; } }