| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2321 人关注过本帖
标题:【求助】求100内的素数
只看楼主 加入收藏
思考ING
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:140
专家分:0
注 册:2008-3-12
收藏
 问题点数:0 回复次数:9 
【求助】求100内的素数
要编一个程序,求100内的所有素数!
本人刚学Java,还不是很懂编程,只知道素数是没有约数的但是具体怎么表达则还不是很清楚~~~~~~~希望知道的告之一下,本人不胜感激~~~~~
搜索更多相关主题的帖子: 素数 约数 Java 
2008-03-29 01:04
llcy
Rank: 1
来 自:成都
等 级:新手上路
帖 子:16
专家分:0
注 册:2008-3-24
收藏
得分:0 
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");
                       
               }                   
           }
    }

}
2008-03-29 09:08
meteor57
Rank: 1
来 自:银河系
等 级:新手上路
帖 子:267
专家分:0
注 册:2007-9-29
收藏
得分:0 
求素数是的约数用'%'求余运算符
用循环表示
让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;
           }
    }
}
2008-03-29 11:35
思考ING
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:140
专家分:0
注 册:2008-3-12
收藏
得分:0 
谢了啊~~~~~~~~~~

天生孤独的思考~~~~~~~~~
2008-03-29 16:06
蔡员外
Rank: 1
来 自:深圳
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-1
收藏
得分:0 
给你更直观点的
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-04-02 11:06
蔡员外
Rank: 1
来 自:深圳
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-4-1
收藏
得分:0 
给你更直观点的
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-04-02 11:07
◎剑魔◎
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-3-31
收藏
得分:0 
meteor57,你将来会成为一名优秀的软件开发人员的——如果你愿意的话!别的没有一点水准可言!
2008-04-02 14:36
meteor57
Rank: 1
来 自:银河系
等 级:新手上路
帖 子:267
专家分:0
注 册:2007-9-29
收藏
得分:0 
[bo]以下是引用 [un]◎剑魔◎[/un] 在 2008-4-2 14:36 的发言:[/bo]

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

......有点受宠若惊了.
我学编程才几个月.但我不是计算机专业的.我学的多是数学.但觉得没什么前途,才希望向这编程方面发展吧.
谢谢你的鼓励.我会继续努力的.希望现在还不晚......
2008-04-02 18:40
meteor57
Rank: 1
来 自:银河系
等 级:新手上路
帖 子:267
专家分:0
注 册:2007-9-29
收藏
得分:0 
回复 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;
           }
    }
}
2008-04-02 18:54
快速回复:【求助】求100内的素数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015123 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved