马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

求素数算法

  [复制链接]
查看5832 | 回复5 | 2013-11-23 18:09:23 | 显示全部楼层 |阅读模式
如何编写一个程序接受用户的数字并显示从零到用户输入数之间的所有质数
ibcadmin | 2013-11-23 20:11:00 | 显示全部楼层
[C#] 纯文本查看 复制代码
  public static int[] PrimeList;
         public static void FindPrime(int n)
         {
              int[] IntList;
              int len=n-1;
              IntList=new int[n];
              for (int p=2;p<=n;p++) IntList[p-1]=p;
              for (int p=2;p<Math.Sqrt(n);p++)
              {
                   if (IntList[p-1]==0) continue;
                   int j=p*p;
                   while (j<=n)
                   {
                       if (IntList[j-1]!=0 )
                       {
                            IntList[j-1]=0;
                            len=len-1;
                       }
                       j=j+p;
                   }
              }
              PrimeList=new int[len];
              int i=0;
              for (int p=2;p<=n;p++)
              {
                   if (IntList[p-1]!=0)
                   {
                       PrimeList[i]=IntList[p-1];
                       i=i+1;
                   }                 
              }
         }
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
在路上 | 2013-12-2 16:27:49 | 显示全部楼层
楼上正解。
断点 | 2014-10-10 16:37:58 | 显示全部楼层
学习
s515562799 | 2015-2-26 14:56:15 | 显示全部楼层
12
s515562799 | 2015-2-26 14:57:03 | 显示全部楼层
房顶上
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则