📌  相关文章
📜  程序计算最多N的不同平方和立方数

📅  最后修改于: 2021-05-06 22:27:00             🧑  作者: Mango

给定数字N,任务是找到从1到给定整数N(均包含)的完全平方数和完全立方体数。
注意:完美平方和完美立方数均应计算一次。
例子:

天真的方法是检查从1到N的所有数字(如果是正方形或立方体)。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
#include  // For sqrt() and cbrt()
using namespace std;
 
// Function to check if the
// number is a perfect square
bool isSquare(int num)
{
    int root = sqrt(num);
    return (root * root) == num;
}
 
// Function to check if the
// number is a perfect cube
bool isCube(int num)
{
    int root = cbrt(num);
    return (root * root * root) == num;
}
 
// Function to count the number
// of perfect squares and cubes
int countSC(int N)
{
    int count = 0;
    for (int i = 1; i <= N; i++) {
 
        // If a number is perfect square,
        if (isSquare(i))
            count++;
 
        // Else if the number is cube or not
        else if (isCube(i))
            count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int N = 20;
 
    cout<< "Number of squares and cubes is " << countSC(N);
    return 0;
}


Java
// Java implementation of
// above approach
class GFG
{
 
// Function to check if the
// number is a perfect square
static boolean isSquare(int num)
{
    int root = (int)Math.sqrt(num);
    return (root * root) == num;
}
 
// Function to check if the
// number is a perfect cube
static boolean isCube(int num)
{
    int root = (int)Math.cbrt(num);
    return (root * root *
            root) == num;
}
 
// Function to count the number
// of perfect squares and cubes
static int countSC(int N)
{
    int count = 0;
    for (int i = 1; i <= N; i++)
    {
 
        // If a number is perfect
        // square,
        if (isSquare(i))
            count++;
 
        // Else if the number is
        // cube or not
        else if (isCube(i))
            count++;
    }
 
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 20;
 
    System.out.println("Number of squares " +
                            "and cubes is " +
                                 countSC(N));
}
}
 
// This code is contributed
// by ChitraNayal


Python3
# Python 3 implementation of
# above approach
 
# Function to check if the
# number is a perfect square
def isSquare(num) :
    root = int(num ** (1 / 2))
 
    return (root * root) == num
 
# Function to check if the
# number is a perfect cube
def isCube(num) :
    root = int(num ** (1 / 3))
    return (root * root * root ) == num
 
# Function to count the number
# of perfect squares and cubes
def countSC(N) :
    count = 0
    for i in range(1, N + 1) :
 
        # If a number is perfect square,
        if isSquare(i) :
            count += 1
             
        # Else if the number is cube
        elif isCube(i) :
            count += 1
             
    return count
 
# Driver code
if __name__ == "__main__" :
 
    N = 20
 
    print("Number of squares and cubes is ",
                                 countSC(N))
             
# This code is contributed by ANKITRAI1


C#
// C# implementation of
// above approach
using System;
 
class GFG
{
 
// Function to check if the
// number is a perfect square
static bool isSquare(int num)
{
    int root = (int)Math.Sqrt(num);
    return (root * root) == num;
}
 
// Function to check if the
// number is a perfect cube
static bool isCube(int num)
{
    int root = (int)Math.Pow(num,
                    (1.0 / 3.0));
    return (root * root * root) == num;
}
 
// Function to count the number
// of perfect squares and cubes
static int countSC(int N)
{
    int count = 0;
    for (int i = 1; i <= N; i++)
    {
 
        // If a number is perfect
        // square,
        if (isSquare(i))
            count++;
 
        // Else if the number is
        // cube or not
        else if (isCube(i))
            count++;
    }
 
    return count;
}
 
// Driver code
public static void Main()
{
    int N = 20;
 
    Console.Write("Number of squares and " +
                  "cubes is " + countSC(N));
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


C++
// C++ implementation of above approach
#include 
#include  // For sqrt() and cbrt()
using namespace std;
 
// Function to count the number
// of perfect squares and cubes
int countSC(int N)
{
    int res = (int)sqrt(N) + (int)cbrt(N)
              - (int)(sqrt(cbrt(N)));
 
    return res;
}
 
// Driver code
int main()
{
    int N = 20;
 
    cout << "Number of squares and cubes is " << countSC(N);
    return 0;
}


Java
// Java implementation of
// above approach
class GFG
{
 
// Function to count the number
// of perfect squares and cubes
static int countSC(int N)
{
    int res = (int)Math.sqrt(N) +
              (int)Math.cbrt(N) -
              (int)(Math.sqrt(Math.cbrt(N)));
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 20;
 
    System.out.println("Number of squares " +
                            "and cubes is " +
                                 countSC(N));
}
}
 
// This code is contributed
// by ChitraNayal


Python3
# Python implementation of
# above approach
import math # for sqrt()
 
# Function to count the number
# of perfect squares and cubes
def countSC(N):
    res = (int(math.sqrt(N)) +
           int(N ** (1 / 3)) -
           int(math.sqrt(N ** (1 / 3))))
 
    return res
 
# Driver code
N = 20
print("Number of squares and cubes is",
                            countSC(N))
 
# This code is contributed
# by vaibhav29498


C#
//C# implementation of
// above approach
using System;
public class GFG {
 
    // Function to count the number
    // of perfect squares and cubes
    static int countSC(int N)
    {
        int res = (int)(Math.Sqrt(N) +
            Math.Ceiling(Math.Pow(N, (double)1 / 3)) -
            (Math.Sqrt(Math.Ceiling(Math.Pow(N, (double)1 / 3)))));
 
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 20;
 
        Console.Write("Number of squares " + "and cubes is " +
                countSC(N));
    }
}
 
 
/*This code is contributed by 29AjayKumar*/


PHP


Javascript


输出:
Number of squares and cubes is 5

高效方法:

  1. 从1到N的平方数是floor(sqrt(N))
  2. 从1到N的多维数据集数是floor(cbrt(N))
  3. 通过从中减去floor(sqrt(cbrt(N)))来消除平方数和立方数(例如1、64 …。)。

下面是上述方法的实现:

C++

// C++ implementation of above approach
#include 
#include  // For sqrt() and cbrt()
using namespace std;
 
// Function to count the number
// of perfect squares and cubes
int countSC(int N)
{
    int res = (int)sqrt(N) + (int)cbrt(N)
              - (int)(sqrt(cbrt(N)));
 
    return res;
}
 
// Driver code
int main()
{
    int N = 20;
 
    cout << "Number of squares and cubes is " << countSC(N);
    return 0;
}

Java

// Java implementation of
// above approach
class GFG
{
 
// Function to count the number
// of perfect squares and cubes
static int countSC(int N)
{
    int res = (int)Math.sqrt(N) +
              (int)Math.cbrt(N) -
              (int)(Math.sqrt(Math.cbrt(N)));
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 20;
 
    System.out.println("Number of squares " +
                            "and cubes is " +
                                 countSC(N));
}
}
 
// This code is contributed
// by ChitraNayal

Python3

# Python implementation of
# above approach
import math # for sqrt()
 
# Function to count the number
# of perfect squares and cubes
def countSC(N):
    res = (int(math.sqrt(N)) +
           int(N ** (1 / 3)) -
           int(math.sqrt(N ** (1 / 3))))
 
    return res
 
# Driver code
N = 20
print("Number of squares and cubes is",
                            countSC(N))
 
# This code is contributed
# by vaibhav29498

C#

//C# implementation of
// above approach
using System;
public class GFG {
 
    // Function to count the number
    // of perfect squares and cubes
    static int countSC(int N)
    {
        int res = (int)(Math.Sqrt(N) +
            Math.Ceiling(Math.Pow(N, (double)1 / 3)) -
            (Math.Sqrt(Math.Ceiling(Math.Pow(N, (double)1 / 3)))));
 
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 20;
 
        Console.Write("Number of squares " + "and cubes is " +
                countSC(N));
    }
}
 
 
/*This code is contributed by 29AjayKumar*/

的PHP


Java脚本


输出:
Number of squares and cubes is 5