在C ++中检查数字是否为勾股定数
在这里,我们将看到另一个程序,用于检查数字是否为毕达哥拉斯素数。在深入探讨逻辑之前,让我们看看毕达哥拉斯素数是什么?毕达哥拉斯质数是质数,可以表示为4n+1。
要检测数字就是这样,我们必须检查数字是否为质数,如果为质数,则将其除以4,如果余数为1,则为毕达哥拉斯质数。毕达哥拉斯质数为{5,13,17,29,37,41,53,…}
示例
#include <iostream>
using namespace std;
bool isPrime(int n){
for(int i = 2; i<= n/2; i++){
if(n % i == 0){
return false;
}
}
return true;
}
bool isPythagoreanPrime(int n) {
if(isPrime(n) && ((n % 4) == 1)){
return true;
}
return false;
}
int main() {
int num = 29;
if(isPythagoreanPrime(num)){
cout << "The number is Pythagorean Prime";
}else{
cout << "The number is not Pythagorean Prime";
}
}输出结果
The number is Pythagorean Prime
热门推荐
10 妈妈生日贺卡祝福语 简短
11 水晶婚夫妻祝福语简短
12 新年祝福语长辈简短红包
13 周二祝福语大全简短
14 上司生日祝福语简短 女性
15 14年祝福语简短情话
16 给舍友送祝福语简短
17 小清新的祝福语简短
18 项目总生日祝福语简短