运维开发网

输出1000以内的素数的算法(实例代码)

运维开发网 https://www.qedev.com 2020-02-13 10:49 出处:网络 作者: 网络整理
本篇文章是对输出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;

}

0

精彩评论

暂无评论...
验证码 换一张
取 消