当前位置: 代码迷 >> Java相关 >> 【求助】求100内的素数
  详细解决方案

【求助】求100内的素数

热度:163   发布时间:2008-03-29 01:04:02.0
【求助】求100内的素数
要编一个程序,求100内的所有素数!
本人刚学Java,还不是很懂编程,只知道素数是没有约数的但是具体怎么表达则还不是很清楚~~~~~~~希望知道的告之一下,本人不胜感激~~~~~
搜索更多相关的解决方案: 素数  约数  Java  

----------------解决方案--------------------------------------------------------
public class Test {

    public static void main(String[] args) {
        // TODO 自动生成方法存根
        int count =0;
        int[] a = new int[100];
           for(int i=0;i<a.length;i++)  a[i]=1+i;//用数组保存1-100的整数
           a[0]=0;//1不是素数,所以设为0
           for(int i=1;i<a.length;i++)
           {
               if(a[i]==0) continue;
               for(int j=i+1;j<a.length;j++)  
                   if(a[j]%a[i]==0)
                       a[j]=0;//是a[i]倍数的元素设为0
           }
           System.out.println(1+"到"+a.length+"之间的素数为: ");
           for(int i=0;i<a.length;i++){
               if(a[i]!=0){
                   System.out.print(a[i]+"\t");
                   count++;
                   if(count%10==0)
                       System.out.println("\t");
                       
               }                   
           }
    }

}
----------------解决方案--------------------------------------------------------
求素数是的约数用'%'求余运算符
用循环表示
让j++;
如果temp%j == 0;
表示temp有约数,约数为j

多看看书,一般书上都会有的.
你的另一个主题我也帮你看了,那个要复杂一点

public class example2 {
    public static void main(String[] args) {
        final int MAX = 100;
        int temp = 1;
        int count = 1;
        System.out.println("100以内的质数是:");
            while(temp <= MAX)
            {
                int j =2;
                while(j <= Math.sqrt(temp) )
                {
                    if(temp%j == 0)
                        break;
                    j++;
                }
            if(j >Math.sqrt(temp))
            {
                System.out.print(temp+"\t");
                count ++;
            }
            if(count%10 == 0)
            {
                System.out.println();
                count = 1;
            }

            if(temp < 3)
                temp ++;
            else temp += 2;
           }
    }
}
----------------解决方案--------------------------------------------------------
谢了啊~~~~~~~~~~
----------------解决方案--------------------------------------------------------
给你更直观点的
class sushu
{
public static void main (String args[])
{
  int i,j;
  for(i=2;i<=100;i++)
   {
    for(j=2;j<i;j++)//出除比它小的数,若余数为零,则为它的约数,即跳出循环。
      if(i%j==0)
      {
      break;
      }
      if(j==i)
      System.out.println(i);
   }
}
}
----------------解决方案--------------------------------------------------------
给你更直观点的
class sushu
{
public static void main (String args[])
{
  int i,j;
  for(i=2;i<=100;i++)
   {
    for(j=2;j<i;j++)//出除比它小的数,若余数为零,则为它的约数,即跳出循环。
      if(i%j==0)
      {
      break;
      }
      if(j==i)
      System.out.println(i);
   }
}
}
----------------解决方案--------------------------------------------------------
meteor57,你将来会成为一名优秀的软件开发人员的――如果你愿意的话!别的没有一点水准可言!
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]◎剑魔◎[/un] 在 2008-4-2 14:36 的发言:[/bo]

meteor57,你将来会成为一名优秀的软件开发人员的――如果你愿意的话!别的没有一点水准可言!

......有点受宠若惊了.
我学编程才几个月.但我不是计算机专业的.我学的多是数学.但觉得没什么前途,才希望向这编程方面发展吧.
谢谢你的鼓励.我会继续努力的.希望现在还不晚......
----------------解决方案--------------------------------------------------------
回复 5# 的帖子
你的虽然直观易懂,但却没有考虑效率.这样对以后写大型程序没什么好处.
又改了一点点.
public class example {
    public static void main(String[] args) {
        final int MAX = 100;
        int temp = 1;
        int count = 1;
        System.out.println("100以内的质数是:");
            while(temp <= MAX)
            {
                int j =2;
                while(j <= Math.sqrt(temp) )
                {
                    if(temp%j == 0)
                        break;
                    if(j < 3)
                        j ++;
                    else
                        j += 2;    
                }
            if(j >Math.sqrt(temp))
            {
                System.out.print(temp+"\t");
                count ++;
            }
            if(count%10 == 0)
            {
                System.out.println();
                count = 1;
            }
            if(temp < 3)
                temp ++;
            else temp += 2;
           }
    }
}
----------------解决方案--------------------------------------------------------
  相关解决方案