码迷,mamicode.com
首页 > 编程语言 > 详细

java怎么求素数

时间:2014-06-30 21:33:45      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   java   color   for   new   

素数:大于1,只能被1和自身整除

 

 

 1 public static boolean isSuShu(int num){
 2         if(num<=1){//大于1
 3             return false;
 4         }
 5         if(num==2||num==3){
 6             return true;
 7         }
 8         int count=1;//你能整除其他值得次数
 9         
10         for(int i=2;i<=num;i++){
11             if(num%i==0){
12                 count++;
13             }
14             if(count>2){//如果你整除的次数大于2,不是素数
15                 return false;
16             }
17         }
18         return true;
19         
20     }

测试就可以了,

public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("请输入一个数");
		int num=input.nextInt();
		System.out.println(isSuShu(num));
		
	}

   请多指教

java怎么求素数,布布扣,bubuko.com

java怎么求素数

标签:style   blog   java   color   for   new   

原文地址:http://www.cnblogs.com/meng70345/p/3816118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!