使用Eratosthenes 方法找出指定范围内的所有质数
来源:互联网 发布:油性漆与水性漆 知乎 编辑:程序博客网 时间:2024/05/17 04:36
//使用Eratosthenes 方法找出指定范围内的所有质数
//貌似是老外写的 ,真心看不懂。。。
#include <stdlib.h>
#define SIZE 20
#define TRUE 1
#define FALSE 0
int main()
{
char sieve[SIZE];/*the sieve*/
char *sp; /*pointer to access the sieve*/
int number; /*number we're computing*/
/*
** Set the entire sieve to TRUE
*/
for(sp = sieve;sp<&sieve[SIZE];)
*sp++ = TRUE;
/*Process each number from 3 to as many as the sieve holds.(Note:the
** loop is terminated from inside.)
*/
for(number =3;;number+=2){
/**
** set the pointer to the proper element in the sieve,and
** stop the loop if we're gone too far.
*/
sp = &sieve[0] + (number-3)/2;
if(sp>=&sieve[SIZE])
break;
/*
**Now advance the pointer by multiples of the number and set
** each subsequent entry FALSE
*/
while(sp += number,sp<&sieve[SIZE])
*sp =FALSE;
}
printf("2\t");
for(number =3,sp =&sieve[0];
sp<&sieve[SIZE];
number +=2, sp++){
if(*sp)
printf("%d\t",number);
}
system("pause");
return EXIT_SUCCESS;
}
//貌似是老外写的 ,真心看不懂。。。
#include <stdlib.h>
#define SIZE 20
#define TRUE 1
#define FALSE 0
int main()
{
char sieve[SIZE];/*the sieve*/
char *sp; /*pointer to access the sieve*/
int number; /*number we're computing*/
/*
** Set the entire sieve to TRUE
*/
for(sp = sieve;sp<&sieve[SIZE];)
*sp++ = TRUE;
/*Process each number from 3 to as many as the sieve holds.(Note:the
** loop is terminated from inside.)
*/
for(number =3;;number+=2){
/**
** set the pointer to the proper element in the sieve,and
** stop the loop if we're gone too far.
*/
sp = &sieve[0] + (number-3)/2;
if(sp>=&sieve[SIZE])
break;
/*
**Now advance the pointer by multiples of the number and set
** each subsequent entry FALSE
*/
while(sp += number,sp<&sieve[SIZE])
*sp =FALSE;
}
printf("2\t");
for(number =3,sp =&sieve[0];
sp<&sieve[SIZE];
number +=2, sp++){
if(*sp)
printf("%d\t",number);
}
system("pause");
return EXIT_SUCCESS;
}
0 0
- 使用Eratosthenes 方法找出指定范围内的所有质数
- 输出指定范围内的所有质数
- 用c语言,找出指定范围内的质数
- python编程找出对应范围内的所有素数(质数)
- 找出从 3 开始到某个数值范围内的所有质数
- 使用函数找出指定范围内的完数
- JavaScript获取指定范围内的质数
- JavaScript求指定范围内的质数
- 用c++建立一个类PrimeNUM,求指定数据范围内的所有素数(质数)。
- 建立一个类PrimeNUM,求指定数据范围内的所有素数(质数)。具体要求如下:
- 使用Eratosthenes筛找出小于等于n的所有素数的C语言实现
- 找出从2到某个数的范围内的所有质数,并输出。要求:分行输出,每行最多10个数。
- 用Java找出输入值范围内的素数(质数)
- 生成质数的好方法:sieve of eratosthenes
- 找出2->N之间的所有质数
- 找出1~100之间的所有质数
- 求1至指定整数范围内的质数
- 求指定范围内的所有素数
- Linux互斥与同步应用(六):文件锁
- 机器学习(写给自己看的)
- HTML中常用的实体字符
- 车牌识别技术详解六--基于Adaboost+haar训练的车牌检测
- 红黑树学习前篇
- 使用Eratosthenes 方法找出指定范围内的所有质数
- nyoj 325 zb的生日 【01-背包】
- 第十七章 路由交换机编程
- SDR SDRAM,DDR1 SDRAM,DDR2 SDRAM和DDR3 SDRAM频率知识区分
- 逃避,是懦弱的表现
- .NET MVC 扩展 HtmlHelper 从无到有(二)[双model,双表单]
- Nike Is Rereleasing Sneakers That Look Like iPhone Cases For Your FeetNike Air Max 2009
- 算法时间复杂度的计算
- 2015 Investment Playbook Small Caps Versus Large Caps ,Chicago Blackhawks