素数筛法/埃拉托斯特尼筛法(sieve of Eratosthenes)——理解及具体问题
来源:互联网 发布:java try单独使用 编辑:程序博客网 时间:2024/06/06 12:39
筛法求素数理解及具体问题
素数筛法的基本思想是:把从1开始的、某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉。剩下的数中选择最小的数是素数,然后去掉它的倍数。依次类推,直到筛子为空时结束。
这里可以列举一下1~20:1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
对于2,3,它的倍数一定不是素数,随意去除掉这些后剩下:
5 7
11 13 17 19
具体举例 ---链接HDU2136
这里可以列举一下1~20:1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
对于2,3,它的倍数一定不是素数,随意去除掉这些后剩下:
5 7
11 13 17 19
具体举例 ---链接HDU2136
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int N=1000005;int a[N];int main(){ int i,j,ans=1;//打表 memset(a,0,sizeof(a)); for(i=2;i<N;i++)//要从二开始,1肯定不行的 { if(a[i]==0) { for( j=i;j<N;j+=i) a[j]=ans; ans++; } } int n; while(~scanf("%d",&n)) printf("%d\n",a[n]); return 0;}待补充
0 0
- 素数筛法/埃拉托斯特尼筛法(sieve of Eratosthenes)——理解及具体问题
- 素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】
- Sieve Of Eratosthenes求素数(埃拉托斯特尼筛法)
- Sieve of Eratosthenes(埃拉托斯特尼素数筛选法)--java实现
- sieve of Eratosthenes——一种寻找素数的方法
- Sieve of Eratosthenes(埃拉托斯尼斯筛法)
- 埃拉托斯特尼筛法(Sieve of Eratosthenes)简单c实现
- 高效寻找质数——Sieve of Eratosthenes
- Sieve of Eratosthenes[ZT]
- The Sieve of Eratosthenes
- Sieve of Eratosthenes
- sieve of Eratosthenes 找质素
- the Sieve of Eratosthenes
- 09 Sieve of Eratosthenes
- Sieve of Eratosthenes
- Sieve of Eratosthenes求质数
- Sieve of Eratosthenes算法实现
- 找质数算法之埃拉托色尼筛选法(Sieve of Eratosthenes算法)
- 第三次上机实验(本月有几天)
- C 笔记-21天学通C++
- AcDbRegion::createFromCurves 引起的内存泄露
- 解决ADT大量出现"Unexpected value from nativeGetEnabledTags: 0"的问题
- 在maven项目中获取配置文件
- 素数筛法/埃拉托斯特尼筛法(sieve of Eratosthenes)——理解及具体问题
- CentOS 6.3 配置VNCServer 5.0.3
- 5-12聊天窗口
- 树、森林与二叉树相互转化原理图
- c++第三次作业
- C++内存泄漏的检测
- iOS部分知识点小结
- Objective-c 对象与类
- Flash如何插入到PowerPoint2003中