33,311
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <cmath>
using namespace std;
bool sushu(int);
int main()
{
int n,flag = 1;
cout<< "2 ";
for (n=3;n<=100;++n)
{
if (n % 2 == 0)
{
continue;
}
if (sushu(n) == false)
{
cout << n << " ";
flag++;
if (flag%5 == 0)
{
cout << endl;
}
}
}
return 0;
}
bool sushu(int n)
{
int i,flag = 0;
for(i=2;i<=n/2;++i)
{
if (n%i==0)
{
flag++;
break;
}
}
if (flag == 0)
return false;
else
return true;
}
#include<stdio.h>
int main()
{
int n1,nm,i,j,flag,count=0;
do{
printf("Input START and END=?\n");
scanf("%d%d",&n1,&nm); /*输入求素数的范围*/
}
while(!(n1>0&&n1<nm)); /*输入正确的范围*/
printf("...........PRIME TABLE(%d--%d)............\n",n1,nm);
if(n1==1||n1==2) /*处理素数2*/
{
printf("%4d",2);
n1=3;count++;
}
for(i=n1;i<=nm;i++) /*判定指定范围内的整数是否为素数*/
{
if(!(i%2))continue;
for(flag=1,j=3;flag&&j<i/2;j+=2)
/*判定能否被从3到整数的一半中的某一数所整除*/
if(!(i%j))flag=0; /*若能整除则不是素数*/
if(flag) printf(++count%5?"%4d":"%4d\n",i);
}
return 0;
}
Input START and END=?
2 100
...........PRIME TABLE(2--100)............
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
Press any key to continue
please input the start and end number:
2 100
........prime number:2-100...........
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
Press any key to continue
#include<iostream>
using namespace std;
void count(int x,int y); //声明count函数
void main()
{
int num1,num2;
cout<<"please input the start and end number:"<<endl;
cin>>num1>>num2;
count(num1,num2); //调用count函数
cout<<endl;
}
void count(int x,int y) //输出素数的函数
{
int flag,num=0;
if (x>0&&x<y) //判断输入的数字合理不
{
cout<<"........prime number:"<<x<<"-"<<y<<"...........\n";
if (x==1||x==2) //如果输入的第一个数小于3,直接输出素数2
{
cout<<"2"<<" ";
num++;
x=3;
}
for (int i=x;i<=y;i++)
{
if (i%2==0) //如果是2的倍数直接跳过
{
continue;
}
flag=3;
while(flag<=i/2 && i%flag!=0) //判断i可以被小于它一半的奇数整除不
{
flag=flag+2;
}
if (flag>i/2) //输出素数
{
num++;
cout<<i<<" ";
if (num%5==0) //每行输出5个
{
cout<<endl;
}
}
}
}
else
{
cout<<"The number is invalid,please INPUT again!";
}
}
#include<stdio.h>
int main()
{
int n1,nm,i,j,flag,count=0;
do{
printf("Input START and END=?");
scanf("%d%d",&n1,&nm); /*输入求素数的范围*/
}
while(!(n1>0&&n1<nm)); /*输入正确的范围*/
printf("...........PRIME TABLE(%d--%d)............\n",n1,nm);
if(n1==1||n1==2) /*处理素数2*/
{
printf("%4d",2);
n1=3;count++;
}
for(i=n1;i<=nm;i++) /*判定指定范围内的整数是否为素数*/
{
if(!(i%2))continue;
for(flag=1,j=3;flag&&j<i/2;j+=2)
/*判定能否被从3到整数的一半中的某一数所整除*/
if(!(i%j))flag=0; /*若能整除则不是素数*/
if(flag) printf(++count%15?"%4d":"%4d\n",i);
}
return 0;
}
Input START and END=?1 1000
...........PRIME TABLE(1--1000)............
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
53 59 61 67 71 73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379
383 389 397 401 409 419 421 431 433 439 443 449 457 461 463
467 479 487 491 499 503 509 521 523 541 547 557 563 569 571
577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733 739 743 751 757 761
769 773 787 797 809 811 821 823 827 829 839 853 857 859 863
877 881 883 887 907 911 919 929 937 941 947 953 967 971 977
983 991 997Press any key to continue