Idoneal Numbers
Last Updated :
01 Dec, 2022
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: Yes
Input: 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++
#include <bits/stdc++.h>
using namespace std;
bool isIdoneal( int n)
{
for ( int a = 1; a <= n; a++) {
for ( int b = a + 1; b <= n; b++) {
for ( int c = b + 1; c <= n; c++) {
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
}
int main()
{
int N = 10;
if (isIdoneal(N))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.lang.*;
class GFG{
static boolean isIdoneal( int n)
{
for ( int a = 1 ; a <= n; a++)
{
for ( int b = a + 1 ; b <= n; b++)
{
for ( int c = b + 1 ; c <= n; c++)
{
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
}
public static void main(String[] args)
{
int N = 10 ;
if (isIdoneal(N))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def isIdoneal(n):
for a in range ( 1 , n + 1 ):
for b in range (a + 1 , n + 1 ):
for c in range (b + 1 , n + 1 ):
if (a * b + b * c + c * a = = n):
return False
return True
N = 10
if (isIdoneal(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isIdoneal( int n)
{
for ( int a = 1; a <= n; a++)
{
for ( int b = a + 1; b <= n; b++)
{
for ( int c = b + 1; c <= n; c++)
{
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
}
public static void Main()
{
int N = 10;
if (isIdoneal(N))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isIdoneal(n)
{
for ( var a = 1; a <= n; a++) {
for ( var b = a + 1; b <= n; b++) {
for ( var c = b + 1; c <= n; c++) {
if (a * b + b * c + c * a == n)
return false ;
}
}
}
return true ;
}
var N = 10;
if (isIdoneal(N))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n*n*n)
Auxiliary Space: O(1)
Reference: https://en.wikipedia.org/wiki/Idoneal_number
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...