在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 虎年新年专属祝福语简短
16 送灯祝福语简短独特
17 新婚过年祝福语大全简短
18 虎年开新车祝福语简短