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
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to check if N is a // Loeschian Number bool isLoeschian( int n)
{ // Iterate [0, sqrt(N)] for x
for ( int x = 1; x <= sqrt (n); x++) {
// Iterate [0, sqrt(N)] for y
for ( int y = 1; y <= sqrt (n); y++) {
// Check the given criteria
if (x * x + x * y + y * y == n)
return true ;
}
}
// If no such pair found then
// return false
return false ;
} // Driver Code int main()
{ // Given Number N
int N = 19;
// Function Call
if (isLoeschian(n))
cout << "Yes" ;
else
cout << "No" ;
} |
// Java program for the above approach class GFG{
// Function to check if N is a // Loeschian Number static boolean isLoeschian( int n)
{ // Iterate [0, sqrt(N)] for x
for ( int x = 1 ; x <= Math.sqrt(n); x++)
{
// Iterate [0, sqrt(N)] for y
for ( int y = 1 ; y <= Math.sqrt(n); y++)
{
// Check the given criteria
if (x * x + x * y + y * y == n)
return true ;
}
}
// If no such pair found then
// return false
return false ;
} // Driver code public static void main(String[] args)
{ // Given Number N
int n = 19 ;
// Function Call
if (isLoeschian(n))
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
} } // This code is contributed by Pratima Pandey |
# Python3 program for the above approach import math
# Function to check if N is a # Loeschian Number def isLoeschian(n):
# Iterate [0, sqrt(N)] for x
for x in range ( 1 , ( int )(math.sqrt(n)) + 1 ):
# Iterate [0, sqrt(N)] for y
for y in range ( 1 , ( int )(math.sqrt(n)) + 1 ):
# Check the given criteria
if (x * x + x * y + y * y = = n):
return True
# If no such pair found then
# return false
return False
# Driver code # Given Number N N = 19
# Function Call if (isLoeschian(N)):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by Vishal Maurya |
// C# program for the above approach using System;
class GFG{
// Function to check if N is a // Loeschian Number static bool isLoeschian( int n)
{ // Iterate [0, sqrt(N)] for x
for ( int x = 1; x <= Math.Sqrt(n); x++)
{
// Iterate [0, sqrt(N)] for y
for ( int y = 1; y <= Math.Sqrt(n); y++)
{
// Check the given criteria
if (x * x + x * y + y * y == n)
return true ;
}
}
// If no such pair found then
// return false
return false ;
} // Driver code public static void Main(String[] args)
{ // Given Number N
int n = 19;
// Function Call
if (isLoeschian(n))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
} } // This code is contributed by amal kumar choubey |
<script> // JavaScript program for the above approach // Function to check if N is a // Loeschian Number function isLoeschian(n) {
// Iterate [0, sqrt(N)] for x
for (let x = 1; x <= Math.sqrt(n); x++) {
// Iterate [0, sqrt(N)] for y
for (let y = 1; y <= Math.sqrt(n); y++) {
// Check the given criteria
if (x * x + x * y + y * y == n)
return true ;
}
}
// If no such pair found then
// return false
return false ;
} // Driver Code // Given Number n let n = 19; // Function Call if (isLoeschian(n))
document.write( "Yes" );
else document.write( "No" );
// This code is contributed by blalverma92 </script> |
Output:
Yes
Time Complexity: O(N)