📜  洛锡安数

📅  最后修改于: 2021-04-26 08:50:04             🧑  作者: Mango

给定数字N ,任务是检查N是否为Loeschian数。如果NLoeschian数,则打印“是”,否则打印“否”

例子:

方法:想法是分别在x和y的[0,sqrt(N)]范围内迭代两个嵌套循环。如果对于任意整数对(x,y)满足方程x^{2} + x*y + y^{2} = N  然后打印“是”,否则打印“否”
下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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
// 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
# 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#
// 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


Javascript


输出:
Yes

时间复杂度: O(N)