输出1000以内的素数的算法(实例代码)
更新时间:2013年05月28日 16:37:08 作者:
本篇文章是对输出1000以内的素数的算法进行了详细的分析介绍,需要的朋友参考下
代码如下所示:
#include "stdafx.h"
#include <iostream>
#include <math.h>
bool IsSushu(int n)
{
bool IsSushuFlg = true;
if( n <= 1)
{
return false;
}
for( int i = 2; i <= (int)sqrt((double)n); i++ )
{
if( 0 == n % i )
{
IsSushuFlg = false;
break;
}
}
return IsSushuFlg;
}
#define N 1000
int main()
{
printf("Su shu is: /n");
for( int i = 2; i < N; i++)
{
bool IsSushuFlg = IsSushu(i);
if( IsSushuFlg )
{
printf("%d /n", i);
}
}
system("pause");
return 0;
}
复制代码 代码如下:
#include "stdafx.h"
#include <iostream>
#include <math.h>
bool IsSushu(int n)
{
bool IsSushuFlg = true;
if( n <= 1)
{
return false;
}
for( int i = 2; i <= (int)sqrt((double)n); i++ )
{
if( 0 == n % i )
{
IsSushuFlg = false;
break;
}
}
return IsSushuFlg;
}
#define N 1000
int main()
{
printf("Su shu is: /n");
for( int i = 2; i < N; i++)
{
bool IsSushuFlg = IsSushu(i);
if( IsSushuFlg )
{
printf("%d /n", i);
}
}
system("pause");
return 0;
}
相关文章
C 与 C++ 中的 const 常量与数组大小的关系对比分析
C和C++中数组大小通常要求是一个编译时常量,C语言中,const变量不能直接作为数组大小,需要使用#define或enum,C++中,const变量被视为常量表达式,可以作为数组大小,本文介绍C 与 C++ 中的 const 常量与数组大小的关系,感兴趣的朋友一起看看吧2025-02-02
C++如何比较两个字符串或string是否相等strcmp()和compare()
这篇文章主要介绍了C++如何比较两个字符串或string是否相等strcmp()和compare()问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-11-11


最新评论