Idoneal Number is a number N if and only if it cannot be written as ab + bc + ca for a > b > c > 0.
Few Idoneal numbers are:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 15, 16, 18, 21, 22…………
Check if a number is an Idoneal number
Given a number N, the task is to check if N is an Idoneal Number or not. If N is an Idoneal Number then print “Yes” else print “No”.
Examples:
Input: N = 10
Output: YesInput: N = 11
Output: No
Approach The idea is to run three nested loops from ‘1’ to ‘N’ and check whether ab + bc + ca equal ‘N’ or not. Return false if ab + bc + ca equal to ‘N’ else return true at the end of the loops.
Below is the implementation of the above approach:
// C++ implementation for the // above approach #include <bits/stdc++.h> using namespace std;
// Function to check if number // is an Idoneal numbers bool isIdoneal( int n)
{ // iterate for all
// triples pairs (a, b, c)
for ( int a = 1; a <= n; a++) {
for ( int b = a + 1; b <= n; b++) {
for ( int c = b + 1; c <= n; c++) {
// if the condition
// is satisfied
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
} // Driver Code int main()
{ int N = 10;
// Function Call
if (isIdoneal(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
} |
// Java implementation for the // above approach import java.lang.*;
class GFG{
// Function to check if number // is an Idoneal numbers static boolean isIdoneal( int n)
{ // Iterate for all
// triples pairs (a, b, c)
for ( int a = 1 ; a <= n; a++)
{
for ( int b = a + 1 ; b <= n; b++)
{
for ( int c = b + 1 ; c <= n; c++)
{
// If the condition
// is satisfied
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
} // Driver Code public static void main(String[] args)
{ int N = 10 ;
// Function Call
if (isIdoneal(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
} } // This code is contributed by rock_cool |
# Python3 implementation for the # above approach # Function to check if number # is an Idoneal numbers def isIdoneal(n):
# Iterate for all
# triples pairs (a, b, c)
for a in range ( 1 , n + 1 ):
for b in range (a + 1 , n + 1 ):
for c in range (b + 1 , n + 1 ):
# If the condition
# is satisfied
if (a * b + b * c + c * a = = n):
return False
return True
# Driver Code N = 10
# Function call if (isIdoneal(N)):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by Vishal Maurya. |
// C# implementation for the // above approach using System;
class GFG{
// Function to check if number // is an Idoneal numbers static bool isIdoneal( int n)
{ // Iterate for all
// triples pairs (a, b, c)
for ( int a = 1; a <= n; a++)
{
for ( int b = a + 1; b <= n; b++)
{
for ( int c = b + 1; c <= n; c++)
{
// If the condition
// is satisfied
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
} // Driver Code public static void Main()
{ int N = 10;
// Function Call
if (isIdoneal(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
} } // This code is contributed by Code_Mech |
<script> // Javascript implementation for the // above approach // Function to check if number // is an Idoneal numbers function isIdoneal(n)
{ // iterate for all
// triples pairs (a, b, c)
for ( var a = 1; a <= n; a++) {
for ( var b = a + 1; b <= n; b++) {
for ( var c = b + 1; c <= n; c++) {
// if the condition
// is satisfied
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
} // Driver Code var N = 10;
// Function Call if (isIdoneal(N))
document.write( "Yes" );
else document.write( "No" );
// This code is contributed by rutvik_56. </script> |
Yes
Time Complexity: O(n*n*n)
Auxiliary Space: O(1)
Reference: https://en.wikipedia.org/wiki/Idoneal_number