Saint-Exupery Numbers
Last Updated :
23 Mar, 2021
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++
#include <bits/stdc++.h>
using namespace std;
bool isSaintExuperyNum( int n)
{
for ( int i = 1; i <= n / 3; i++) {
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 ;
}
int main()
{
int N = 60;
if (isSaintExuperyNum(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean isSaintExuperyNum( int n)
{
for ( int i = 1 ; i <= n / 3 ; i++)
{
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 ;
}
public static void main(String[] args)
{
int N = 60 ;
if (isSaintExuperyNum(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isSaintExuperyNum(n):
for i in range ( 1 , (n / / 3 ) + 1 ):
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
N = 60
if isSaintExuperyNum(N):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isSaintExuperyNum( int n)
{
for ( int i = 1; i <= n / 3; i++)
{
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 ;
}
public static void Main()
{
int N = 60;
if (isSaintExuperyNum(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isSaintExuperyNum( n) {
for ( i = 1; i <= n / 3; i++) {
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 ;
}
let N = 60;
if (isSaintExuperyNum(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n^2)
Reference: http://www.numbersaplenty.com/set/Saint-Exupery_number/
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...