嵌套循环程序设计
7-9 梅森数
#include<stdio.h>
//#include<math.h>
int suShu(int number){
for(int i=2;i*i<=number;i++){//是小于等于,用i*i降低时间复杂度
if(number % i == 0){
return 0;
}
}
return 1;
}//检查素数
int meisenShu(int number,int n){
for(int i=2;i<=n;i++){
if((number + 1) == 1 << i ){//移位运算,<<右移,表示2的n次方
return 1;
}
}
return 0;
}//是2的n次方-1
//主程序要满足suShu,meisenShu
int main (){
int n, total;
int flag = 0;
scanf("%d",&n);
// total = pow(2,n) - 1;
total = (1 << n) - 1;
for(int i=2;i<=total;i++){
if(meisenShu(i,n) && suShu(i)){
flag = 1;//找到梅森数
printf("%d\n",i);
}
}
if(flag == 0){
printf("None");
}
return 0;
}
标签:int,number,梅森数,嵌套循环,suShu,include
From: https://www.cnblogs.com/GJ504b/p/18539887