c语言题目,求大家帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/05/26 05:25:52
输入2个整数n1〈 n2,编程计算正整数n1到n2之间的素数之和

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
unsigned int function1(unsigned int arg)
{
unsigned int up ,i = 2;
sqrt(2);
up = (int)(sqrt(arg));
if(arg <= 1) return 0;
while(i<=up)
{
if(arg%i == 0) return 0;
i++;
}
return 1;
}

void main()
{
unsigned int i,p,q;
long sum = 0;
printf("Pls input p,q:");
do
{
scanf("%d%d",&p,&q);
if(p>q) printf("\nERROR INPUT!\n");
}while(p>q);
i=p;
while(i<=q)
{
if(function1(i))
{
printf("%d ",i);
sum+=i;
}
i++;
}
printf("\nSum = %ld\n",sum);
}
如果不包括p,q(即n1,n2),那就让i从q+1开始到q-1结束

zhishu(int m)
{
int j,t=1;
for(j=2;j<m;j++)
{if(m%j==0)
t=0;}
return t;
}