Open In App

Saint-Exupery Numbers

Last Updated : 23 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Saint-Exupery Number a number N such that N is the product of the three sides of Pythagorean triangle.
Some of the Saint-Exupery numbers are: 
 

60, 480, 780, 1620, 2040, 3840, 4200, 6240, 7500, 12180…. 
 

 

Check if N is a Saint Exupery number

Given a number N, the task is to check if N is a Saint-Exupery Number or not. If N is an Saint-Exupery Number then print “Yes” else print “No”.
Examples: 
 

Input: N = 60 
Output: Yes 
Explanation: 
60 = 3 * 4 * 5 and 3^2 + 4^2 = 5^2.
Input: N = 120 
Output: No 
 

Naive Approach: A simple solution is to run three nested loops to generate all possible triplets and for every triplet, check if it is a Pythagorean Triplet and has given product. Time complexity of this solution is O(n3).
Efficient Approach: The idea is to run two loops, where first loop runs from i = 1 to n/3, second loop runs from j = i+1 to n/2. In second loop, we check if (n / i / j) is equal to i * i + j * j.
Below is the implementation of the above approach:
 

C++




// C++ implementation to check if N
// is a Saint-Exupery number
 
#include <bits/stdc++.h>
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




<script>
 
// Javascript program for above approach
 
 
    // Function to check if a number is
    // a Saint-Exupery number
    function isSaintExuperyNum( n) {
        // Considering triplets in
        // sorted order. The value
        // of first element in sorted
        // triplet can be at-most n/3.
        for ( i = 1; i <= n / 3; i++) {
 
            // The value of second
            // element must be less
            // than equal to n/2
            for ( j = i + 1; j <= n / 2; j++) {
                let 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
        let N = 60;
 
        // Function Call
        if (isSaintExuperyNum(N))
            document.write("Yes");
        else
            document.write("No");
 
// This code contributed by gauravrajput1
 
</script>


Output: 

Yes

 

Time Complexity: O(n^2) 
Reference: http://www.numbersaplenty.com/set/Saint-Exupery_number/
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads