Program to count number of distinct Squares and Cubes upto N

Given a number N, the task is to find No. of perfect Squares and perfect Cubes from 1 to a given integer, N ( Both Inclusive ).

Note: Numbers which are both perfect Square and perfect Cube should be counted once.

Examples:

Input: N = 70
Output: 10
Numbers that are perfect Square or perfect Cube
1, 4, 8, 9, 16, 25, 27, 36, 49, 64

Input: N = 25
Output: 6
Numbers that are perfect Square or perfect Cube
1, 4, 8, 9, 16, 25

A Naive approach is to check all numbers from 1 to N, if it is a square or cube.

Below is the implementation of above apporoach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <iostream>
#include <math.h> // 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP implementation of above approach
  
// Function to check if the
// number is a perfect square
function isSquare($num)
{
    $root = (int)sqrt($num);
    return ($root * $root) == $num;
}
  
// Function to check if the
// number is a perfect cube
function isCube($num)
{
    $root = (int)pow($num, 1 / 3);
    return ($root * $root *
            $root) == $num;
}
  
// Function to count the number
// of perfect squares and cubes
function countSC($N)
{
    $count = 0;
    for ($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
$N = 20;
  
echo "Number of squares and "
     "cubes is " . countSC($N);
  
// This code is contributed 
// by ChitraNayal
?>

chevron_right


Output:

Number of squares and cubes is 5

Efficient Approach:

  1. Number of squares from 1 to N is floor(sqrt(N)).
  2. Number of cubes from 1 to N is floor(cbrt(N)).
  3. Eliminate the numbers which are both square and cube ( like 1, 64…. ) by subtracting floor(sqrt(cbrt(N))) from it.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <iostream>
#include <math.h> // 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//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*/

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Function to count the number
// of perfect squares and cubes
function countSC($N)
{
    $res = sqrt($N) + pow($N, 1 / 3) - 
                (sqrt(pow($N, 1 / 3)));
  
    return floor($res);
}
  
// Driver code
$N = 20;
  
echo "Number of squares and cubes is " ,
                            countSC($N);
  
// This code is contributed 
// by Shashank
?>

chevron_right


Output:

Number of squares and cubes is 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.