Given a number N, the task is to check if N is an Loeschian Number or not. If N is an Loeschian Number then print “Yes” else print “No”.
A number N is a Loeschian Number if N can be expressed of the form for any two integers x and y.
Examples:
Input: N = 19
Output:Yes
Explanation:
19 = 22+ 2*3 +32
Input: N = 20
Output: No
Approach: The idea is to iterate two nested loops in the range [0, sqrt(N)] for x and y respectively. If for any pairs of integers (x, y) satisfy the equation then print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isLoeschian( int n)
{
for ( int x = 1; x <= sqrt (n); x++) {
for ( int y = 1; y <= sqrt (n); y++) {
if (x * x + x * y + y * y == n)
return true ;
}
}
return false ;
}
int main()
{
int N = 19;
if (isLoeschian(n))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
class GFG{
static boolean isLoeschian( int n)
{
for ( int x = 1 ; x <= Math.sqrt(n); x++)
{
for ( int y = 1 ; y <= Math.sqrt(n); y++)
{
if (x * x + x * y + y * y == n)
return true ;
}
}
return false ;
}
public static void main(String[] args)
{
int n = 19 ;
if (isLoeschian(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
import math
def isLoeschian(n):
for x in range ( 1 , ( int )(math.sqrt(n)) + 1 ):
for y in range ( 1 , ( int )(math.sqrt(n)) + 1 ):
if (x * x + x * y + y * y = = n):
return True
return False
N = 19
if (isLoeschian(N)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isLoeschian( int n)
{
for ( int x = 1; x <= Math.Sqrt(n); x++)
{
for ( int y = 1; y <= Math.Sqrt(n); y++)
{
if (x * x + x * y + y * y == n)
return true ;
}
}
return false ;
}
public static void Main(String[] args)
{
int n = 19;
if (isLoeschian(n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
<script>
function isLoeschian(n) {
for (let x = 1; x <= Math.sqrt(n); x++) {
for (let y = 1; y <= Math.sqrt(n); y++) {
if (x * x + x * y + y * y == n)
return true ;
}
}
return false ;
}
let n = 19;
if (isLoeschian(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N)
Last Updated :
05 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...