Loeschian Number
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++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to check if N is a// Loeschian Numberbool 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 Codeint main(){ // Given Number N int N = 19; // Function Call if (isLoeschian(n)) cout << "Yes"; else cout << "No";} |
Java
// Java program for the above approachclass GFG{// Function to check if N is a// Loeschian Numberstatic 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 codepublic 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
# Python3 program for the above approachimport math# Function to check if N is a# Loeschian Numberdef 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 NN = 19# Function Callif (isLoeschian(N)): print("Yes")else: print("No")# This code is contributed by Vishal Maurya |
C#
// C# program for the above approachusing System;class GFG{// Function to check if N is a// Loeschian Numberstatic 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 codepublic 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 |
Javascript
<script>// JavaScript program for the above approach// Function to check if N is a// Loeschian Numberfunction 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 nlet n = 19;// Function Callif (isLoeschian(n)) document.write("Yes");else document.write("No");// This code is contributed by blalverma92</script> |
Output:
Yes
Time Complexity: O(N)


