How do you find squares and cubes in python?

Here, we are going to execute a python program to discover the square and 3D shape of a given number by making capacities.

Given a number, and we need to compose client characterized capacities to locate the square and 3D square of the number is Python.

Example:

 Input:
    Enter an integer number: 6

    Output:
    Square of 6 is 36
    Cube of 6 is 216

Function to get square:

def  square (num):
	return (num*num)

Function to get cube:

def  cube (num):
	return (num*num*num)

Program:

# python program to find square and cube
# of a given number

# User defind method to find square 
def square (num):
	return  (num*num)

# User defind method to find cube
def cube (num) :
	return  (num*num*num) 

# Main code 
# input a number
number = int (raw_input("Enter an integer number: "))

# square and cube
print "square of {0} is {1}".format(number, square(number))
print "Cube of {0} is {1}".format(number, cube (number))

Output

 Enter an integer number: 6
    square of 6 is 36
    Cube of 6 is 216

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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 
    Explanation: Numbers that are perfect Square or perfect Cube 
    1, 4, 8, 9, 16, 25, 27, 36, 49, 64

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

    Naive Approach: 

    Check all numbers from 1 to N, if it is a square or cube.

    Below is the implementation of above approach:  

    C++

    #include <iostream>

    #include <math.h> // For sqrt() and cbrt()

    using namespace std;

    bool isSquare(int num)

    {

        int root = sqrt(num);

        return (root * root) == num;

    }

    bool isCube(int num)

    {

        int root = cbrt(num);

        return (root * root * root) == num;

    }

    int countSC(int N)

    {

        int count = 0;

        for (int i = 1; i <= N; i++) {

            if (isSquare(i))

                count++;

            else if (isCube(i))

                count++;

        }

        return count;

    }

    int main()

    {

        int N = 20;

        cout << "Number of squares and cubes is " << countSC(N);

        return 0;

    }

    Java

    class GFG {

        static boolean isSquare(int num)

        {

            int root = (int)Math.sqrt(num);

            return (root * root) == num;

        }

        static boolean isCube(int num)

        {

            int root = (int)Math.cbrt(num);

            return (root * root * root) == num;

        }

        static int countSC(int N)

        {

            int count = 0;

            for (int i = 1; i <= N; i++) {

                if (isSquare(i))

                    count++;

                else if (isCube(i))

                    count++;

            }

            return count;

        }

        public static void main(String[] args)

        {

            int N = 20;

            System.out.println("Number of squares "

                               + "and cubes is " + countSC(N));

        }

    }

    Python3

    def isSquare(num):

        root = int(num ** (1 / 2))

        return (root * root) == num

    def isCube(num):

        root = int(num ** (1 / 3))

        return (root * root * root) == num

    def countSC(N):

        count = 0

        for i in range(1, N + 1):

            if isSquare(i):

                count += 1

            elif isCube(i):

                count += 1

        return count

    if __name__ == "__main__":

        N = 20

        print("Number of squares and cubes is ",

              countSC(N))

    C#

    using System;

    class GFG {

        static bool isSquare(int num)

        {

            int root = (int)Math.Sqrt(num);

            return (root * root) == num;

        }

        static bool isCube(int num)

        {

            int root = (int)Math.Pow(num, (1.0 / 3.0));

            return (root * root * root) == num;

        }

        static int countSC(int N)

        {

            int count = 0;

            for (int i = 1; i <= N; i++) {

                if (isSquare(i))

                    count++;

                else if (isCube(i))

                    count++;

            }

            return count;

        }

        public static void Main()

        {

            int N = 20;

            Console.Write("Number of squares and "

                          + "cubes is " + countSC(N));

        }

    }

    PHP

    <?php

    function isSquare($num)

    {

        $root = (int)sqrt($num);

        return ($root * $root) == $num;

    }

    function isCube($num)

    {

        $root = (int)pow($num, 1 / 3);

        return ($root * $root *

                $root) == $num;

    }

    function countSC($N)

    {

        $count = 0;

        for ($i = 1; $i <= $N; $i++)

        {

            if (isSquare($i))

                $count++;

            else if (isCube($i))

                $count++;

        }

        return $count;

    }

    $N = 20;

    echo "Number of squares and " .

         "cubes is " . countSC($N);

    ?>

    Javascript

    <script>

        function isSquare(num)

        {

            let root = parseInt(Math.sqrt(num), 10);

            return (root * root) == num;

        }

        function isCube(num)

        {

            let root = parseInt(Math.cbrt(num), 10);

            return (root * root * root) == num;

        }

        function countSC(N)

        {

            let count = 0;

            for (let i = 1; i <= N; i++) {

                if (isSquare(i))

                    count++;

                else if (isCube(i))

                    count++;

            }

            return count;

        }

        let N = 20;

        document.write("Number of squares and cubes is " + countSC(N));

    </script>

    Output

    Number of squares and cubes is 5

    Time Complexity: O(N)
    Space Complexity: O(1) 

    Efficient Approach: 

    • Number of squares from 1 to N is floor(sqrt(N)).
    • Number of cubes from 1 to N is floor(cbrt(N)).
    • 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++

    #include <iostream>

    #include <math.h> // For sqrt() and cbrt()

    using namespace std;

    int countSC(int N)

    {

        int res = (int)sqrt(N) + (int)cbrt(N)

                  - (int)(sqrt(cbrt(N)));

        return res;

    }

    int main()

    {

        int N = 20;

        cout << "Number of squares and cubes is " << countSC(N);

        return 0;

    }

    Java

    class GFG {

        static int countSC(int N)

        {

            int res = (int)Math.sqrt(N) + (int)Math.cbrt(N)

                      - (int)(Math.sqrt(Math.cbrt(N)));

            return res;

        }

        public static void main(String[] args)

        {

            int N = 20;

            System.out.println("Number of squares "

                               + "and cubes is " + countSC(N));

        }

    }

    Python3

    import math 

    def cr(N):

        if pow(round(N ** (1 / 3)), 3) == N:

            return round(N ** (1 / 3))

        return int(N ** (1 / 3))

    def countSC(N):

        res = (int(math.sqrt(N)) +

               int(cr(N)) -

               int(math.sqrt(cr(N))))

        return res

    N = 20

    print("Number of squares and cubes is",

          countSC(N))

    C#

    using System;

    public class GFG {

        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;

        }

        public static void Main()

        {

            int N = 20;

            Console.Write("Number of squares "

                          + "and cubes is " + countSC(N));

        }

    }

    PHP

    <?php

    function countSC($N)

    {

        $res = sqrt($N) + pow($N, 1 / 3) -

                    (sqrt(pow($N, 1 / 3)));

        return floor($res);

    }

    $N = 20;

    echo "Number of squares and cubes is " ,

                                countSC($N);

    ?>

    Javascript

    <script>

        function countSC(N)

        {

            let res = parseInt(Math.sqrt(N), 10) + parseInt(Math.cbrt(N), 10) - parseInt(Math.sqrt(parseInt(Math.cbrt(N), 10)), 10);

            return res;

        }

        let N = 20;

        document.write("Number of squares and cubes is " + countSC(N));

    </script>

    Output

    Number of squares and cubes is 5

    Time Complexity: O(1)
    Auxiliary Space: O(1)


    How do you find a square and a cube in Python?

    Python Write functions to find the square and cube of a given....
    Example: Input: Enter an integer number: 6 Output: Square of 6 is 36 Cube of 6 is 216..
    Function to get square: def square (num): return (num*num).
    Function to get cube: def cube (num): return (num*num*num).

    How do you find squares in Python?

    How to Square a Number in Python – Squaring Function.
    **, the power operator..
    the in-built pow() function..
    the math. pow() function from the math module..

    How do you find the cube in Python?

    Python Program to Find Cube of a Number.
    def cube(x):.
    return x * x * x..
    n = int(input(" Enter the number : ")).
    cube1 = cube(n).
    print("The Cube of {0} = {1}". format(n, cube1)).

    Is there any cube function in Python?

    The pow() function finds the cube of a number by giving the values of i and number. ex: pow(i,3).