思考ING 发表于 2008-3-29 01:04

【求助】求100内的素数

要编一个程序,求100内的所有素数!
本人刚学Java,还不是很懂编程,只知道素数是没有约数的但是具体怎么表达则还不是很清楚~~~~~~~希望知道的告之一下,本人不胜感激~~~~~

llcy 发表于 2008-3-29 09:08

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");
                                          
                           }                                  
                   }
        }

}

meteor57 发表于 2008-3-29 11:35

求素数是的约数用'%'求余运算符
用循环表示
让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;
                   }
        }
}

思考ING 发表于 2008-3-29 16:06

谢了啊~~~~~~~~~~

蔡员外 发表于 2008-4-2 11:06

给你更直观点的
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);
   }
}
}

蔡员外 发表于 2008-4-2 11:07

给你更直观点的
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);
   }
}
}

◎剑魔◎ 发表于 2008-4-2 14:36

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

meteor57 发表于 2008-4-2 18:40

[quote][bo]以下是引用 [un]◎剑魔◎[/un] 在 2008-4-2 14:36 的发言:[/bo]

meteor57,你将来会成为一名优秀的软件开发人员的——如果你愿意的话!别的没有一点水准可言! [/quote]
......有点受宠若惊了.
我学编程才几个月.但我不是计算机专业的.我学的多是数学.但觉得没什么前途,才希望向这编程方面发展吧.
谢谢你的鼓励.我会继续努力的.希望现在还不晚......

meteor57 发表于 2008-4-2 18:54

回复 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;
           }
    }
}

页: [1]

编程论坛