📜  完美魔方

📅  最后修改于: 2021-04-29 03:49:42             🧑  作者: Mango

给定一个数字N ,任务是检查给定的数字N是否是一个完美的立方体。
例子:

方法1:天真的方法
这个想法是检查从1N的每个数字,如果这些数字中任何一个的立方等于N。如果是这样,则该数字是N的立方根,而N是一个理想的立方。

下面是上述方法的实现:

C++
// C++ program to check whether the given
// number N is the perfect cube or not
.
#include 
    using namespace std;
 
// Function to check if a number
// is a perfect Cube or not
void perfectCube(int N)
{
    int cube;
 
    // Iterate from 1-N
    for (int i; i <= N; i++) {
 
        // Find the cube of
        // every number
        cube = i * i * i;
 
        // Check if cube equals
        // N or not
        if (cube == N) {
            cout << "Yes";
            return;
        }
        else if (cube > N) {
            cout << "NO";
            return;
        }
    }
}
 
// Driver code
int main()
{
    int N = 216;
 
    // Function call
    perfectCube(N);
 
    return 0;
}


Java
// Java program to check whether the given
// number N is the perfect cube or not
class GFG {
     
    // Function to check if a number
    // is a perfect Cube or not
    static void perfectCube(int N)
    {
        int cube;
     
        // Iterate from 1-N
        for (int i = 0; i <= N; i++) {
     
            // Find the cube of
            // every number
            cube = i * i * i;
     
            // Check if cube equals
            // N or not
            if (cube == N) {
                System.out.println("Yes");
                return;
            }
            else if (cube > N) {
                System.out.println("NO");
                return;
            }
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int N = 216;
     
        // Function call
        perfectCube(N);
 
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to check whether the given
# number N is the perfect cube or not
 
# Function to check if a number
# is a perfect Cube or not
def perfectCube(N) :
 
    cube = 0;
 
    # Iterate from 1-N
    for i in range(N + 1) :
 
        # Find the cube of
        # every number
        cube = i * i * i;
 
        # Check if cube equals
        # N or not
        if (cube == N) :
            print("Yes");
            return;
     
        elif (cube > N) :
            print("NO");
            return;
 
# Driver code
if __name__ == "__main__" :
 
    N = 216;
 
    # Function call
    perfectCube(N);
 
# This code is contributed  by Yash_R


C#
// C# program to check whether the given
// number N is the perfect cube or not
using System;
 
class GFG {
     
    // Function to check if a number
    // is a perfect Cube or not
    static void perfectCube(int N)
    {
        int cube;
     
        // Iterate from 1-N
        for (int i = 0; i <= N; i++) {
     
            // Find the cube of
            // every number
            cube = i * i * i;
     
            // Check if cube equals
            // N or not
            if (cube == N) {
                Console.WriteLine("Yes");
                return;
            }
            else if (cube > N) {
                Console.WriteLine("NO");
                return;
            }
        }
    }
     
    // Driver code
    public static void Main (string[] args)
    {
        int N = 216;
     
        // Function call
        perfectCube(N);
 
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ program to check whether the given
// number N is the perfect cube or not
 
#include 
using namespace std;
 
// Function to check if a number is
// a perfect Cube using inbuilt function
void perfectCube(int N)
{
    int cube_root;
    cube_root = round(cbrt(N));
 
    // If cube of cube_root is equals to N,
    // then print Yes Else print No
    if (cube_root * cube_root * cube_root == N) {
        cout << "Yes";
        return;
    }
    else {
        cout << "NO";
        return;
    }
}
 
// Driver's code
int main()
{
    int N = 125;
 
    // Function call to check
    // N is cube or not
    perfectCube(N);
    return 0;
}


Java
// Java program to check whether the given
// number N is the perfect cube or not
public class GFG {
 
    // Function to check if a number is
    // a perfect Cube using inbuilt function
    static void perfectCube(int N)
    {
        int cube_root;
        cube_root = (int)Math.round(Math.cbrt(N));
     
        // If cube of cube_root is equals to N,
        // then print Yes Else print No
        if (cube_root * cube_root * cube_root == N) {
            System.out.println("Yes");
            return;
        }
        else {
            System.out.println("NO");
            return;
        }
    }
     
    // Driver's code
    public static void main (String[] args)
    {
        int N = 125;
     
        // Function call to check
        // N is cube or not
        perfectCube(N);
     
    }
 
}
// This code is contributed by AnkitRai01


Python3
# Python program to check whether the given
# number N is the perfect cube or not
 
# Function to check if a number is
# a perfect Cube using inbuilt function
def perfectCube(N) :
 
    cube_root = round(N**(1/3));
 
    # If cube of cube_root is equals to N,
    # then print Yes Else print No
    if cube_root * cube_root * cube_root == N :
        print("Yes");
        return;
 
    else :
        print("NO");
        return;
 
# Driver's code
if __name__ == "__main__" :
    N = 125;
 
    # Function call to check
    # N is cube or not
    perfectCube(N);
 
# This code is contributed by AnkitRai01


C#
// C# program to check whether the given
// number N is the perfect cube or not
using System;
 
class GFG {
 
    // Function to check if a number is
    // a perfect Cube using inbuilt function
    static void perfectCube(int N)
    {
        int cube_root;
        cube_root = (int)Math.Round(Math.Cbrt(N));
     
        // If cube of cube_root is equals to N,
        // then print Yes Else print No
        if (cube_root * cube_root * cube_root == N) {
            Console.WriteLine("Yes");
            return;
        }
        else {
            Console.WriteLine("NO");
            return;
        }
    }
     
    // Driver's code
    public static void Main (string[] args)
    {
        int N = 125;
     
        // Function call to check
        // N is cube or not
        perfectCube(N);
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ program to check if a number
// is a perfect cube using prime factors
#include
using namespace std;
 
// Inserts the prime factor in HashMap
// if not present
// if present updates it's frequency
map insertPF(map primeFact,
                           int fact)
{
    if (primeFact.find(fact) != primeFact.end())
    {
        primeFact[fact]++;
    }
    else
    {
        primeFact[fact] = 1;
    }
    return primeFact;
}
 
// A utility function to find all
// prime factors of a given number N
map primeFactors (int n)
{
    map primeFact;
 
    // Insert the number of 2s
    // that divide n
    while (n % 2 == 0)
    {
        primeFact = insertPF(primeFact, 2);
        n /= 2;
    }
 
    // n must be odd at this point
    // So we can skip one element
    for(int i = 3; i <= sqrt(n); i += 2)
    {
 
        // while i divides n, insert
        // i and divide n
        while (n % i == 0)
        {
            primeFact = insertPF(primeFact, i);
            n /= i;
        }
    }
 
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2
    if (n > 2)
        primeFact = insertPF(primeFact, n);
 
    return primeFact;
}
 
// Function to check if a
// number is perfect cube
string perfectCube (int n)
{
    map primeFact;
    primeFact = primeFactors(n);
 
    // Iteration in Map
    for(auto x : primeFact)
    {
        if (x.second % 3 != 0)
            return "No";
    }
    return "Yes";
}
 
// Driver Code
int main()
{
    int N = 216;
 
    // Function to check if N is
    // perfect cube or not
    cout << perfectCube(N);
 
    return 0;
}
 
// This code is contributed by himanshu77


Java
// Java program to check if a number
// is a perfect cube using prime factors
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Inserts the prime factor in the Hash Map
    // if not present
    // If present updates it's frequency
    public static HashMap
    insertPF(HashMap primeFact,
             int fact)
    {
        if (primeFact.containsKey(fact)) {
 
            int freq;
            freq = primeFact.get(fact);
            primeFact.replace(fact, ++freq);
        }
        else {
            primeFact.put(fact, 1);
        }
        return primeFact;
    }
 
    // A utility function to find all
    // prime factors of a given number N
    public static HashMap
    primeFactors(int n)
    {
 
        HashMap primeFact
            = new HashMap<>();
 
        // Insert the number of 2s
        // that divide n
        while (n % 2 == 0) {
            primeFact = insertPF(primeFact, 2);
            n /= 2;
        }
 
        // n must be odd at this point.
        // So we can skip one element
        for (int i = 3; i <= Math.sqrt(n);
             i += 2) {
 
            // While i divides n, insert i
            // and divide n
            while (n % i == 0) {
                primeFact = insertPF(primeFact, i);
                n /= i;
            }
        }
 
        // This condition is to handle
        // the case when n is a prime
        // number greater than 2
        if (n > 2)
            primeFact = insertPF(primeFact, n);
 
        return primeFact;
    }
 
    // Function to check if a number
    // is a perfect cube
    public static String perfectCube(int n)
    {
 
        HashMap primeFact;
        primeFact = primeFactors(n);
 
        // Using values() for iteration
        // over keys
        for (int freq : primeFact.values()) {
            if (freq % 3 != 0)
                return "No";
        }
        return "Yes";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 216;
 
        // Function to check if N is
        // perfect cube or not
        System.out.println(perfectCube(N));
    }
}


Python3
# Python3 program to check if a number
# is a perfect cube using prime factors
import math
 
# Inserts the prime factor in HashMap
# if not present
# if present updates it's frequency
def insertPF(primeFact, fact) :
 
    if (fact in primeFact) :
     
        primeFact[fact] += 1
      
    else :
      
        primeFact[fact] = 1
 
    return primeFact
   
# A utility function to find all
# prime factors of a given number N
def primeFactors (n) :
 
    primeFact = {}
   
    # Insert the number of 2s
    # that divide n
    while (n % 2 == 0) :
     
        primeFact = insertPF(primeFact, 2)
        n = n // 2
   
    # n must be odd at this point
    # So we can skip one element
    for i in range(3, int(math.sqrt(n)) + 1, 2) :
   
        # while i divides n, insert
        # i and divide n
        while (n % i == 0) :
     
            primeFact = insertPF(primeFact, i)
            n = n // i
   
    # This condition is to handle 
    # the case when n is a prime
    # number greater than 2
    if (n > 2) :
        primeFact = insertPF(primeFact, n)
   
    return primeFact
   
# Function to check if a 
# number is perfect cube
def perfectCube (n) :
 
    primeFact = {}
    primeFact = primeFactors(n)
   
    # Iteration in Map
    for x in primeFact :
     
        if (primeFact[x] % 3 != 0) :
            return "No"
      
    return "Yes"
   
N = 216
 
# Function to check if N is
# perfect cube or not
print(perfectCube(N))
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program to check if a number
// is a perfect cube using prime factors
 
using System;
using System.Collections.Generic;
 
public class GFG {
  
    // Inserts the prime factor in the Hash Map
    // if not present
    // If present updates it's frequency
    public static Dictionary
    insertPF(Dictionary primeFact,
             int fact)
    {
        if (primeFact.ContainsKey(fact)) {
  
            int freq;
            freq = primeFact[fact];
            primeFact[fact] = ++freq;
        }
        else {
            primeFact.Add(fact, 1);
        }
        return primeFact;
    }
  
    // A utility function to find all
    // prime factors of a given number N
    public static Dictionary
    primeFactors(int n)
    {
  
        Dictionary primeFact
            = new Dictionary();
  
        // Insert the number of 2s
        // that divide n
        while (n % 2 == 0) {
            primeFact = insertPF(primeFact, 2);
            n /= 2;
        }
  
        // n must be odd at this point.
        // So we can skip one element
        for (int i = 3; i <= Math.Sqrt(n);
             i += 2) {
  
            // While i divides n, insert i
            // and divide n
            while (n % i == 0) {
                primeFact = insertPF(primeFact, i);
                n /= i;
            }
        }
  
        // This condition is to handle
        // the case when n is a prime
        // number greater than 2
        if (n > 2)
            primeFact = insertPF(primeFact, n);
  
        return primeFact;
    }
  
    // Function to check if a number
    // is a perfect cube
    public static String perfectCube(int n)
    {
  
        Dictionary primeFact;
        primeFact = primeFactors(n);
  
        // Using values() for iteration
        // over keys
        foreach (int freq in primeFact.Values) {
            if (freq % 3 != 0)
                return "No";
        }
        return "Yes";
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 216;
  
        // Function to check if N is
        // perfect cube or not
        Console.WriteLine(perfectCube(N));
    }
}
 
// This code is contributed by sapnasingh4991


输出:
Yes

时间复杂度: O(N)

辅助空间: O(1)

方法2:使用内置函数
这个想法是使用内置函数( cbrt() )查找某个数字的立方根,该立方根返回数字N的立方根的底值。如果此数字的立方等于N ,则N是一个理想的立方,否则N不是一个理想的立方。

下面是上述方法的实现:

C++

// C++ program to check whether the given
// number N is the perfect cube or not
 
#include 
using namespace std;
 
// Function to check if a number is
// a perfect Cube using inbuilt function
void perfectCube(int N)
{
    int cube_root;
    cube_root = round(cbrt(N));
 
    // If cube of cube_root is equals to N,
    // then print Yes Else print No
    if (cube_root * cube_root * cube_root == N) {
        cout << "Yes";
        return;
    }
    else {
        cout << "NO";
        return;
    }
}
 
// Driver's code
int main()
{
    int N = 125;
 
    // Function call to check
    // N is cube or not
    perfectCube(N);
    return 0;
}

Java

// Java program to check whether the given
// number N is the perfect cube or not
public class GFG {
 
    // Function to check if a number is
    // a perfect Cube using inbuilt function
    static void perfectCube(int N)
    {
        int cube_root;
        cube_root = (int)Math.round(Math.cbrt(N));
     
        // If cube of cube_root is equals to N,
        // then print Yes Else print No
        if (cube_root * cube_root * cube_root == N) {
            System.out.println("Yes");
            return;
        }
        else {
            System.out.println("NO");
            return;
        }
    }
     
    // Driver's code
    public static void main (String[] args)
    {
        int N = 125;
     
        // Function call to check
        // N is cube or not
        perfectCube(N);
     
    }
 
}
// This code is contributed by AnkitRai01

Python3

# Python program to check whether the given
# number N is the perfect cube or not
 
# Function to check if a number is
# a perfect Cube using inbuilt function
def perfectCube(N) :
 
    cube_root = round(N**(1/3));
 
    # If cube of cube_root is equals to N,
    # then print Yes Else print No
    if cube_root * cube_root * cube_root == N :
        print("Yes");
        return;
 
    else :
        print("NO");
        return;
 
# Driver's code
if __name__ == "__main__" :
    N = 125;
 
    # Function call to check
    # N is cube or not
    perfectCube(N);
 
# This code is contributed by AnkitRai01

C#

// C# program to check whether the given
// number N is the perfect cube or not
using System;
 
class GFG {
 
    // Function to check if a number is
    // a perfect Cube using inbuilt function
    static void perfectCube(int N)
    {
        int cube_root;
        cube_root = (int)Math.Round(Math.Cbrt(N));
     
        // If cube of cube_root is equals to N,
        // then print Yes Else print No
        if (cube_root * cube_root * cube_root == N) {
            Console.WriteLine("Yes");
            return;
        }
        else {
            Console.WriteLine("NO");
            return;
        }
    }
     
    // Driver's code
    public static void Main (string[] args)
    {
        int N = 125;
     
        // Function call to check
        // N is cube or not
        perfectCube(N);
    }
}
 
// This code is contributed by AnkitRai01

Java脚本


输出:
Yes

时间复杂度: O(cbrt(N))

辅助空间: O(1)

方法3:使用总理因素

  1. 使用本文中的方法找到给定数N的所有素数。
  2. 将以上获得的所有主要因子的频率存储在哈希图中。
  3. 遍历哈希图,如果每个素数的频率都不是3的倍数,则给定的数字N不是理想的立方体。

下面是上述方法的实现:

C++

// C++ program to check if a number
// is a perfect cube using prime factors
#include
using namespace std;
 
// Inserts the prime factor in HashMap
// if not present
// if present updates it's frequency
map insertPF(map primeFact,
                           int fact)
{
    if (primeFact.find(fact) != primeFact.end())
    {
        primeFact[fact]++;
    }
    else
    {
        primeFact[fact] = 1;
    }
    return primeFact;
}
 
// A utility function to find all
// prime factors of a given number N
map primeFactors (int n)
{
    map primeFact;
 
    // Insert the number of 2s
    // that divide n
    while (n % 2 == 0)
    {
        primeFact = insertPF(primeFact, 2);
        n /= 2;
    }
 
    // n must be odd at this point
    // So we can skip one element
    for(int i = 3; i <= sqrt(n); i += 2)
    {
 
        // while i divides n, insert
        // i and divide n
        while (n % i == 0)
        {
            primeFact = insertPF(primeFact, i);
            n /= i;
        }
    }
 
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2
    if (n > 2)
        primeFact = insertPF(primeFact, n);
 
    return primeFact;
}
 
// Function to check if a
// number is perfect cube
string perfectCube (int n)
{
    map primeFact;
    primeFact = primeFactors(n);
 
    // Iteration in Map
    for(auto x : primeFact)
    {
        if (x.second % 3 != 0)
            return "No";
    }
    return "Yes";
}
 
// Driver Code
int main()
{
    int N = 216;
 
    // Function to check if N is
    // perfect cube or not
    cout << perfectCube(N);
 
    return 0;
}
 
// This code is contributed by himanshu77

Java

// Java program to check if a number
// is a perfect cube using prime factors
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Inserts the prime factor in the Hash Map
    // if not present
    // If present updates it's frequency
    public static HashMap
    insertPF(HashMap primeFact,
             int fact)
    {
        if (primeFact.containsKey(fact)) {
 
            int freq;
            freq = primeFact.get(fact);
            primeFact.replace(fact, ++freq);
        }
        else {
            primeFact.put(fact, 1);
        }
        return primeFact;
    }
 
    // A utility function to find all
    // prime factors of a given number N
    public static HashMap
    primeFactors(int n)
    {
 
        HashMap primeFact
            = new HashMap<>();
 
        // Insert the number of 2s
        // that divide n
        while (n % 2 == 0) {
            primeFact = insertPF(primeFact, 2);
            n /= 2;
        }
 
        // n must be odd at this point.
        // So we can skip one element
        for (int i = 3; i <= Math.sqrt(n);
             i += 2) {
 
            // While i divides n, insert i
            // and divide n
            while (n % i == 0) {
                primeFact = insertPF(primeFact, i);
                n /= i;
            }
        }
 
        // This condition is to handle
        // the case when n is a prime
        // number greater than 2
        if (n > 2)
            primeFact = insertPF(primeFact, n);
 
        return primeFact;
    }
 
    // Function to check if a number
    // is a perfect cube
    public static String perfectCube(int n)
    {
 
        HashMap primeFact;
        primeFact = primeFactors(n);
 
        // Using values() for iteration
        // over keys
        for (int freq : primeFact.values()) {
            if (freq % 3 != 0)
                return "No";
        }
        return "Yes";
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 216;
 
        // Function to check if N is
        // perfect cube or not
        System.out.println(perfectCube(N));
    }
}

Python3

# Python3 program to check if a number
# is a perfect cube using prime factors
import math
 
# Inserts the prime factor in HashMap
# if not present
# if present updates it's frequency
def insertPF(primeFact, fact) :
 
    if (fact in primeFact) :
     
        primeFact[fact] += 1
      
    else :
      
        primeFact[fact] = 1
 
    return primeFact
   
# A utility function to find all
# prime factors of a given number N
def primeFactors (n) :
 
    primeFact = {}
   
    # Insert the number of 2s
    # that divide n
    while (n % 2 == 0) :
     
        primeFact = insertPF(primeFact, 2)
        n = n // 2
   
    # n must be odd at this point
    # So we can skip one element
    for i in range(3, int(math.sqrt(n)) + 1, 2) :
   
        # while i divides n, insert
        # i and divide n
        while (n % i == 0) :
     
            primeFact = insertPF(primeFact, i)
            n = n // i
   
    # This condition is to handle 
    # the case when n is a prime
    # number greater than 2
    if (n > 2) :
        primeFact = insertPF(primeFact, n)
   
    return primeFact
   
# Function to check if a 
# number is perfect cube
def perfectCube (n) :
 
    primeFact = {}
    primeFact = primeFactors(n)
   
    # Iteration in Map
    for x in primeFact :
     
        if (primeFact[x] % 3 != 0) :
            return "No"
      
    return "Yes"
   
N = 216
 
# Function to check if N is
# perfect cube or not
print(perfectCube(N))
 
# This code is contributed by divyeshrabadiya07.

C#

// C# program to check if a number
// is a perfect cube using prime factors
 
using System;
using System.Collections.Generic;
 
public class GFG {
  
    // Inserts the prime factor in the Hash Map
    // if not present
    // If present updates it's frequency
    public static Dictionary
    insertPF(Dictionary primeFact,
             int fact)
    {
        if (primeFact.ContainsKey(fact)) {
  
            int freq;
            freq = primeFact[fact];
            primeFact[fact] = ++freq;
        }
        else {
            primeFact.Add(fact, 1);
        }
        return primeFact;
    }
  
    // A utility function to find all
    // prime factors of a given number N
    public static Dictionary
    primeFactors(int n)
    {
  
        Dictionary primeFact
            = new Dictionary();
  
        // Insert the number of 2s
        // that divide n
        while (n % 2 == 0) {
            primeFact = insertPF(primeFact, 2);
            n /= 2;
        }
  
        // n must be odd at this point.
        // So we can skip one element
        for (int i = 3; i <= Math.Sqrt(n);
             i += 2) {
  
            // While i divides n, insert i
            // and divide n
            while (n % i == 0) {
                primeFact = insertPF(primeFact, i);
                n /= i;
            }
        }
  
        // This condition is to handle
        // the case when n is a prime
        // number greater than 2
        if (n > 2)
            primeFact = insertPF(primeFact, n);
  
        return primeFact;
    }
  
    // Function to check if a number
    // is a perfect cube
    public static String perfectCube(int n)
    {
  
        Dictionary primeFact;
        primeFact = primeFactors(n);
  
        // Using values() for iteration
        // over keys
        foreach (int freq in primeFact.Values) {
            if (freq % 3 != 0)
                return "No";
        }
        return "Yes";
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 216;
  
        // Function to check if N is
        // perfect cube or not
        Console.WriteLine(perfectCube(N));
    }
}
 
// This code is contributed by sapnasingh4991
输出:
Yes