求:3--100之间的素数.怎么写啊。在线等待。

sht9911 2004-10-12 09:24:47
我自己写了一下,可是就是不能求出来。大家给看看。
#include<iostream.h>
void main()
{
int i,j;
for(i=3;i<=100;i++)
{
for(j=3;j<=i;j++)
{
if(i%(j-1)==0)

continue;
}cout<<i<<endl;
}
}
...全文
291 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangfjj 2004-10-13
  • 打赏
  • 举报
回复
怎么不能用?
sqrt????
zhangfjj 2004-10-13
  • 打赏
  • 举报
回复
sqrt
sht9911 2004-10-13
  • 打赏
  • 举报
回复
谢谢楼上的!结贴给分了。
wuxiyang_2004_10 2004-10-13
  • 打赏
  • 举报
回复
怎么不能用?
sqrt????
————————————————————
必须在开头引用c++数学函数库
#include "math.h"
wuxiyang_2004_10 2004-10-13
  • 打赏
  • 举报
回复
#include "iostream.h"
#include "math.h"
int su(int x)//自定义函数su(),用来判断一个整数是不是素数,如果是则返回1,不是则返回0
{
int i,zhi;
for(i=2;i<=sqrt(x);i++)
{
if(x%i==0)
{
zhi=0;
break;
}
else if(i>=sqrt(x))
{
zhi=1;
}
}
return zhi;
}
void main()
{
int n;
for(n=3;n<=100;n++)
{
if(su(n))//调用函数su(),判断n是否为素数
cout<<n<<" ";
}
}



yxh36 2004-10-12
  • 打赏
  • 举报
回复
#include <iostream>
#include <iomanip>
using namespace std;
#include <cmath>
void main()
{
int l=0;
for(int m=3;m<=100;m+=2)
{
int sqrtm=sqrt(m);
for(int i=2;i<=sqrtm;i++)
if(m%i==0)
break;
if(i>sqrtm)
{
if(l++%10==0)
cout<<endl;
cout<<setw(5)<<m;
}
}
}还有就是提示你哈,这些个在书上都有的,谭浩强 版的c语言版!!上面有!!
jp1984 2004-10-12
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>
#include <malloc.h>
void print(int* p,int n)
{
int i,cnt = 0;
for(i = 0;i < n;i++){
if(p[i] != 0){
printf("%d ",p[i]);
cnt++;
if(cnt % 5 == 0)
putchar('\n');
}
}
} /* print all the primes */

void sieve(int a,int b,int* p)
{
int c = b - a; /* how many numbers in the interval */
int m = a,i,n = 2;
for(i = 0;i < c;i++)
{
p[i] = m;
m++;
}
while(n < b)
{
for(i = 0;i < c;i++)
if(p[i] % n == 0 && p[i] != n)
p[i] = 0;

n++;
}
print(p,c);
}
int main(void)
{
int a,b,c;

printf("Enter the interval : ");
scanf("%d%d",&a,&b);
c = b - a;
int* p = malloc(c);
sieve(a,b,p);

return 0;
}
[james@Mathematcs basic]$ gcc -o test findPrimes.c
[james@Mathematcs basic]$ ./test
Enter the interval : 3 100
3 5 7 11 13
17 19 23 29 31
37 41 43 47 53
59 61 67 71 73
79 83 89 97 [james@Mathematcs basic]$

/* kao ,wei shen me bu neng yong sqrt a ?*/
LEO8410 2004-10-12
  • 打赏
  • 举报
回复
//倘若要是求数a到b怎么写呢;

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int sushu(int);
void main()
{
int a,b,l=0;
do
{
cout<<"请输入两个数:";
cin>>a>>b;
}while(a>b);
//上面用这个do...while是判断a是否小于b!
cout<<"SuShu"<<a<<"to"<<b<<"is:\n";
if(a%2==0)//是判断输入的数是奇数还是偶数;是偶数加一;
a++;
for(int i=a;i<=b;i+=2)//这地方i+=2,是因为我们知道偶数一定不是素数!
if(sushu(i))
{
cout<<setw(5)<<i;
l++;
if(l%8==0)
cout<<endl;
}
cout<<endl;
}
int sushu(int m)
{
int sqrtm=sqrt(m);
for(int i=2;i<=sqrtm;i++)
if(m%i==0)
break;
if(sqrtm<i)
return true;
else
return false;
}

agaric 2004-10-12
  • 打赏
  • 举报
回复
先编个验证质数的函数
验证一个数x是不素就是用 2~x/2去求余数
jf_zhang 2004-10-12
  • 打赏
  • 举报
回复
请问一下楼上,#include <iomanip> using namespace std; #include <cmath>是什么意思,是不是在用线程解决问题
sht9911 2004-10-12
  • 打赏
  • 举报
回复
为什么要用int main(). 而我用void main()就不行呢?这是什么意思?
LEO8410 2004-10-12
  • 打赏
  • 举报
回复
#include <iostream>
#include <iomanip>
using namespace std;
#include <cmath>
void main()
{
int l=0;
for(int m=3;m<=100;m+=2)
{
int sqrtm=sqrt(m);
for(int i=2;i<=sqrtm;i++)
if(m%i==0)
break;
if(i>sqrtm)
{
if(l++%10==0)
cout<<endl;
cout<<setw(5)<<m;
}
}
}
双杯献酒 2004-10-12
  • 打赏
  • 举报
回复
// Dev C++ 4.9.8.0
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
int i,j;
for(i=3;i<=100;i++)
{
for(j=3;j<=i;j++)
{
if(i%(j-1)==0)break;
if(j==i) cout<<i<<endl;
}
}
system("pause");
return 0;
}

/*
结果:
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
请按任意键继续 . . .
*/
cxjddd 2004-10-12
  • 打赏
  • 举报
回复
for (j = 2; j < i; ++j)
if (i % j == 0)
break;
if (j == i)
cout << i << endl;
cxjddd 2004-10-12
  • 打赏
  • 举报
回复
int main() *_*

你应该用 break 替换掉 continue,这样如果有因子的话,j 循环就会在中间退出,然后判断 j 是否达到 i 就可以知道是不是素数了
zhangfjj 2004-10-12
  • 打赏
  • 举报
回复
你没有弄懂什么是素数,及算法是怎么的

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧