📌  相关文章
📜  检查数字是否可以由两个平方的乘积表示

📅  最后修改于: 2021-04-23 19:40:14             🧑  作者: Mango

给定一个整数n ,我们的任务是检查数字n是否可以由两个平方的乘积表示。如果可能,则打印“是”,否则打印“否”。
例子 :

天真的方法:
为了解决上述问题,朴素的方法是使用蛮力法。使用两个进行循环迭代直到n,每次我们将检查两个数字的平方的乘积是否等于N。如果找到这样的组合,则将打印“是”,否则打印“否”。

下面是上述方法的实现:

C++
// C++ implementation to Check whether a number can
// be represented by the product of two squares
#include 
using namespace std;
 
// Function to check if there exist two
// numbers product of whose squares is n.
bool prodSquare(int n)
{
    for (long i = 2; i * i <= n; i++)
 
        for (long j = 2; j <= n; j++)
 
            // check whether the product of the square
            // of both numbers is equal to N
            if (i * i * j * j == n)
                return true;
 
    return false;
}
 
// Driver code
int main()
{
    int n = 25;
    if (prodSquare(n))
        cout << "Yes";
 
    else
        cout << "No";
}


Java
// Java implementation to check whether a number can
// be represented by the product of two squares
class GFG{
 
// Function to check if there exist two
// numbers product of whose squares is n.
static boolean prodSquare(int n)
{
    for (long i = 2; i * i <= n; i++)
 
        for (long j = 2; j <= n; j++)
 
            // Check whether the product of the square
            // of both numbers is equal to N
            if (i * i * j * j == n)
                return true;
                 
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 25;
    if (prodSquare(n))
        System.out.print("Yes");
 
    else
        System.out.print("No");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 implementation to check whether
# a number can be represented by the
# product of two squares
 
# Function to check if there exist two
# numbers product of whose squares is n.
def prodSquare(n):
     
    for i in range(2, (n) + 1):
        if(i * i < (n + 1)):
            for j in range(2, n + 1):
                 
                # Check whether the product
                # of the square of both
                # numbers is equal to N
                if ((i * i * j * j) == n):
                    return True;
    return False;
 
# Driver code
if __name__ == '__main__':
     
    n = 25;
     
    if (prodSquare(n)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Rajput-Ji


C#
// C# implementation to check whether
// a number can be represented by
// the product of two squares
using System;
 
class GFG{
 
// Function to check if there
// exist two numbers product
// of whose squares is n.
static bool prodSquare(int n)
{
    for(long i = 2; i * i <= n; i++)
 
       for(long j = 2; j <= n; j++)
        
          // Check whether the product
          // of the square of both
          // numbers is equal to N
          if (i * i * j * j == n)
              return true;
                 
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 25;
     
    if (prodSquare(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by sapnasingh4991


Javascript


C++
// C++ implementation to Check whether a number can
// be represented by the product of two squares
#include 
using namespace std;
 
// Function to check if there exist two
// numbers product of whose squares is n
bool prodSquare(int n)
{
    // Initialize map
    unordered_map s;
 
    for (int i = 2; i * i <= n; ++i) {
 
        // Store square value in hashmap
        s[i * i] = 1;
 
        if (s.find(n / (i * i)) != s.end())
            return true;
    }
    return false;
}
 
// Driver code
int main()
{
    int n = 25;
 
    if (prodSquare(n))
        cout << "Yes";
 
    else
        cout << "No";
}


Java
// Java implementation to check whether
// a number can be represented by the
// product of two squares
import java.util.*;
 
class GFG{
 
// Function to check if there exist two
// numbers product of whose squares is n
static boolean prodSquare(int n)
{
     
    // Initialize map
    HashMap s = new HashMap();
 
    for(int i = 2; i * i <= n; ++i)
    {
        
       // Store square value in hashmap
       s.put((float)(i * i), (float) 1);
        
       if (s.containsKey((float) n / (i * i)))
           return true;
    }
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 25;
 
    if (prodSquare(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to check whether
# a number can be represented by the
# product of two squares
 
# Function to check if there exist two
# numbers product of whose squares is n
def prodSquare(n):
 
    # Initialize dict/map
    s = dict()
 
    i = 2
    while (i * i <= n):
 
        # Store square value in hashmap
        s[i * i] = 1
 
        if ((n // (i * i)) in s):
            return True
 
        i += 1
 
    return False
 
# Driver Code
if __name__ == '__main__':
     
    n = 25
 
    if (prodSquare(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by himanshu77


C#
// C# implementation to check whether
// a number can be represented by the
// product of two squares
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if there exist two
// numbers product of whose squares is n
static bool prodSquare(int n)
{
     
    // Initialize map
    Dictionary s = new Dictionary();
    for(int i = 2; i * i <= n; ++i)
    {
        
       // Store square value in hashmap
       s.Add((float)(i * i), (float) 1);
        
       if (s.ContainsKey((float) n / (i * i)))
           return true;
    }
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 25;
 
    if (prodSquare(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by amal kumar choubey


输出:
No

时间复杂度: O(n)

高效方法:
要优化上述方法,请使用一个哈希图,其中将存储直到sqrt(n)的数字平方,并且每次我们在哈希图中搜索(n / sqrt(i))(如果存在),然后返回Yes(是),否则返回No(否)。

C++

// C++ implementation to Check whether a number can
// be represented by the product of two squares
#include 
using namespace std;
 
// Function to check if there exist two
// numbers product of whose squares is n
bool prodSquare(int n)
{
    // Initialize map
    unordered_map s;
 
    for (int i = 2; i * i <= n; ++i) {
 
        // Store square value in hashmap
        s[i * i] = 1;
 
        if (s.find(n / (i * i)) != s.end())
            return true;
    }
    return false;
}
 
// Driver code
int main()
{
    int n = 25;
 
    if (prodSquare(n))
        cout << "Yes";
 
    else
        cout << "No";
}

Java

// Java implementation to check whether
// a number can be represented by the
// product of two squares
import java.util.*;
 
class GFG{
 
// Function to check if there exist two
// numbers product of whose squares is n
static boolean prodSquare(int n)
{
     
    // Initialize map
    HashMap s = new HashMap();
 
    for(int i = 2; i * i <= n; ++i)
    {
        
       // Store square value in hashmap
       s.put((float)(i * i), (float) 1);
        
       if (s.containsKey((float) n / (i * i)))
           return true;
    }
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 25;
 
    if (prodSquare(n))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation to check whether
# a number can be represented by the
# product of two squares
 
# Function to check if there exist two
# numbers product of whose squares is n
def prodSquare(n):
 
    # Initialize dict/map
    s = dict()
 
    i = 2
    while (i * i <= n):
 
        # Store square value in hashmap
        s[i * i] = 1
 
        if ((n // (i * i)) in s):
            return True
 
        i += 1
 
    return False
 
# Driver Code
if __name__ == '__main__':
     
    n = 25
 
    if (prodSquare(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by himanshu77

C#

// C# implementation to check whether
// a number can be represented by the
// product of two squares
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if there exist two
// numbers product of whose squares is n
static bool prodSquare(int n)
{
     
    // Initialize map
    Dictionary s = new Dictionary();
    for(int i = 2; i * i <= n; ++i)
    {
        
       // Store square value in hashmap
       s.Add((float)(i * i), (float) 1);
        
       if (s.ContainsKey((float) n / (i * i)))
           return true;
    }
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 25;
 
    if (prodSquare(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by amal kumar choubey
输出:
No

时间复杂度: O(sqrt n)