首页 > 其他 > 详细

打印100~200 之间的素数,两种方法

时间:2015-11-02 06:45:27      阅读:177      评论:0      收藏:0      [点我收藏+]

解:方法1c语言编程:

#include <stdio.h>

#include <math.h>

int main()

{

int i=0;

    int count=0;

for(i=101;i<=199;i+=2)

{

int j=0;

for(j=3;j<=0.5*sqrt(i);j+=2)

{

if(i%j==0)

{

break;

}

}

if(j>0.5*sqrt(i))

{

count++;

printf("%d  ",i);

}

}

printf("count=%d\n",count);

return 0;

}

输出结果:

101  103  107  109  113  119  121  127  131  133  137  139  143  149  151  157  161  163  167  169  173  179  181  187  191  193  197  199  count=28

Press any key to continue

c++编程:

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

int i=0;

    int count=0;

for(i=101;i<=199;i+=2)

{

int j=0;

for(j=3;j<=0.5*sqrt(i);j+=2)

{

if(i%j==0)

{

break;

}

}

if(j>0.5*sqrt(i))

{

count++;

cout<<i<<" ";

}

}

cout<<count<<endl;

return 0;

}

输出结果:

101 103 107 109 113 119 121 127 131 133 137 139 143 149 151 157 161 163 167 169 173 179 181 187 191 193 197 199 28

Press any key to continue

方法2c语言编程

#include<stdio.h>

#include<math.h>

main()

{   

    int i,k,tag,j=0;

    for (i=100;i<=200;i++)

{tag=0;

      for(k=2;k<i;k++)

        if(i%k==0)tag=1;

        if(tag==0)

{printf("%d ",i); j++;

if(j%5==0)printf("\n");

}  

}

}

输出结果:

101 103 107 109 113

127 131 137 139 149

151 157 163 167 173

179 181 191 193 197

199 Press any key to continue


本文出自 “51cto” 博客,请务必保留此出处http://51cccto.blog.51cto.com/10251929/1708670

打印100~200 之间的素数,两种方法

原文:http://51cccto.blog.51cto.com/10251929/1708670

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!