圣艾修伯里数字N ,使得N是毕达哥拉斯三角形的三个边的乘积。
圣艾修伯里的一些数字是:
60, 480, 780, 1620, 2040, 3840, 4200, 6240, 7500, 12180….
检查N是否为圣艾修伯里号
给定数字N ,任务是检查N是否为圣艾修伯里数字。如果N是圣艾修伯里数字,则打印“是”,否则打印“否” 。
例子:
Input: N = 60
Output: Yes
Explanation:
60 = 3 * 4 * 5 and 3^2 + 4^2 = 5^2.
Input: N = 120
Output: No
天真的方法:一个简单的解决方案是运行三个嵌套循环以生成所有可能的三元组,对于每个三元组,检查它是否为毕达哥拉斯三元组并提供了乘积。该解决方案的时间复杂度为O(n 3 )。
高效方法:这个想法是运行两个循环,其中第一个循环从i = 1到n / 3,第二个循环从j = i + 1到n / 2。在第二个循环中,我们检查(n / i / j)是否等于i * i + j * j。
下面是上述方法的实现:
C++
// C++ implementation to check if N
// is a Saint-Exupery number
#include
using namespace std;
// Function to check if a number is
// a Saint-Exupery number
bool isSaintExuperyNum(int n)
{
// Considering triplets in
// sorted order. The value
// of first element in sorted
// triplet can be at-most n/3.
for (int i = 1; i <= n / 3; i++) {
// The value of second
// element must be less
// than equal to n/2
for (int j = i + 1; j <= n / 2; j++) {
int k = n / i / j;
if (i * i + j * j == k * k) {
if (i * j * k == n)
return true;
;
}
}
}
return false;
}
// Driver Code
int main()
{
// Given Number N
int N = 60;
// Function Call
if (isSaintExuperyNum(N))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for above approach
class GFG{
// Function to check if a number is
// a Saint-Exupery number
static boolean isSaintExuperyNum(int n)
{
// Considering triplets in
// sorted order. The value
// of first element in sorted
// triplet can be at-most n/3.
for (int i = 1; i <= n / 3; i++)
{
// The value of second
// element must be less
// than equal to n/2
for (int j = i + 1; j <= n / 2; j++)
{
int k = n / i / j;
if (i * i + j * j == k * k)
{
if (i * j * k == n)
return true;
}
}
}
return false;
}
// Driver Code
public static void main(String[] args)
{
// Given Number N
int N = 60;
// Function Call
if (isSaintExuperyNum(N))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Shubham Prakash
Python3
# Python3 implementation to check if N
# is a Saint-Exupery number
# Function to check if a number is
# a Saint-Exupery number
def isSaintExuperyNum(n):
# Considering triplets in
# sorted order. The value
# of first element in sorted
# triplet can be at-most n/3.
for i in range(1, (n // 3) + 1):
# The value of second
# element must be less
# than equal to n/2
for j in range(i + 1, (n // 2) + 1):
k = n / i / j
if i * i + j * j == k * k:
if i * j * k == n:
return True
return False
# Driver Code
# Given Number N
N = 60
# Function Call
if isSaintExuperyNum(N):
print("Yes")
else:
print("No")
# This code is contributed by
# divyamohan123
C#
// C# program for above approach
using System;
class GFG{
// Function to check if a number is
// a Saint-Exupery number
static bool isSaintExuperyNum(int n)
{
// Considering triplets in
// sorted order. The value
// of first element in sorted
// triplet can be at-most n/3.
for (int i = 1; i <= n / 3; i++)
{
// The value of second
// element must be less
// than equal to n/2
for (int j = i + 1; j <= n / 2; j++)
{
int k = n / i / j;
if (i * i + j * j == k * k)
{
if (i * j * k == n)
return true;
}
}
}
return false;
}
// Driver Code
public static void Main()
{
// Given Number N
int N = 60;
// Function Call
if (isSaintExuperyNum(N))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes
时间复杂度: O(n ^ 2)
参考:http://www.numbersaplenty.com/set/Saint-Exupery_number/