A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 单曲循环 中级黑马   /  2014-8-18 08:26  /  912 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

题目:判断101-200之间有多少个素数,并输出所有素数。   
1.程序分析:判断素数的方法:用一个数分别去除2sqrt(这个数),如果能被整除,   
则表明此数不是素数,反之是素数。
public class exp2{
    public static void main(String args[]){
       int i=0;
       math mymath = new math();
       for(i=2;i<=200;i++)
           if(mymath.iszhishu(i)==true)
           System.out.println(i);
    }
}
class math
{
    public int f(int x)
    {
       if(x==1 || x==2)
           return 1;
       else
           return f(x-1)+f(x-2);
    }
    public boolean iszhishu(int x)
    {
       for(int i=2;i<=x/2;i++)
           if (x % 2==0 )
              return false;
       return true;
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马