Previous perfect square and cube number smaller than number N

03 Sep, 2021
Given an integer N, the task is to find the previous perfect square or perfect cube smaller than the number N.Examples: 
Input: N = 6 Output: Perfect Square = 4 Perfect Cube = 1Input: N = 30 Output: Perfect Square = 25 Perfect Cube = 27 
Approach: Previous perfect square number less than N can be computed as follows:  
Find the square root of given number N.
Calculate its floor value using floor function of the respective language.
Then subtract 1 from it if N is already a perfect square.
Print square of that number.
Previous perfect cube number less than N can be computed as follows:  
Find the cube root of given N.
Calculate its floor value using floor function of the respective language.
Then subtract 1 from it if N is already a perfect cube.
Print cube of that number.
Below is the implementation of above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation to find the// previous perfect square and cube// smaller than the given number #include <cmath>#include <iostream> using namespace std; // Function to find the previous// perfect square of the number Nint previousPerfectSquare(int N){    int prevN = floor(sqrt(N));         // If N is already a perfect square    // decrease prevN by 1.    if (prevN * prevN == N)        prevN -= 1;     return prevN * prevN;} // Function to find the// previous perfect cubeint previousPerfectCube(int N){    int prevN = floor(cbrt(N));         // If N is already a perfect cube    // decrease prevN by 1.    if (prevN * prevN * prevN == N)        prevN -= 1;             return prevN * prevN * prevN;} // Driver Codeint main(){    int n = 30;    cout << previousPerfectSquare(n) << "\n";    cout << previousPerfectCube(n) << "\n";    return 0;}
// Java implementation to find the// previous perfect square and cube// smaller than the given numberimport java.util.*; class GFG{ // Function to find the previous// perfect square of the number Nstatic int previousPerfectSquare(int N){    int prevN = (int)Math.floor(Math.sqrt(N));         // If N is already a perfect square    // decrease prevN by 1.    if (prevN * prevN == N)        prevN -= 1;     return prevN * prevN;} // Function to find the// previous perfect cubestatic int previousPerfectCube(int N){    int prevN = (int)Math.floor(Math.cbrt(N));         // If N is already a perfect cube    // decrease prevN by 1.    if (prevN * prevN * prevN == N)        prevN -= 1;             return prevN * prevN * prevN;} // Driver Codepublic static void main(String[] args){    int n = 30;    System.out.println(previousPerfectSquare(n));    System.out.println(previousPerfectCube(n));}} // This code is contributed by Rohit_ranjan
# Python3 implementation to find the# previous perfect square and cube# smaller than the given numberimport mathimport numpy as np # Function to find the previous# perfect square of the number Ndef previousPerfectSquare(N):     prevN = math.floor(math.sqrt(N));         # If N is already a perfect square    # decrease prevN by 1.    if (prevN * prevN == N):        prevN -= 1;     return prevN * prevN; # Function to find the# previous perfect cubedef previousPerfectCube(N):     prevN = math.floor(np.cbrt(N));         # If N is already a perfect cube    # decrease prevN by 1.    if (prevN * prevN * prevN == N):        prevN -= 1;             return prevN * prevN * prevN; # Driver Coden = 30; print(previousPerfectSquare(n));print(previousPerfectCube(n)); # This code is contributed by Code_Mech
// C# implementation to find the// previous perfect square and cube// smaller than the given numberusing System; class GFG{ // Function to find the previous// perfect square of the number Nstatic int previousPerfectSquare(int N){    int prevN = (int)Math.Floor(Math.Sqrt(N));         // If N is already a perfect square    // decrease prevN by 1.    if (prevN * prevN == N)        prevN -= 1;     return prevN * prevN;} // Function to find the// previous perfect cubestatic int previousPerfectCube(int N){    int prevN = (int)Math.Floor(Math.Cbrt(N));         // If N is already a perfect cube    // decrease prevN by 1.    if (prevN * prevN * prevN == N)        prevN -= 1;             return prevN * prevN * prevN;} // Driver Codepublic static void Main(String[] args){    int n = 30;         Console.WriteLine(previousPerfectSquare(n));    Console.WriteLine(previousPerfectCube(n));}} // This code is contributed by sapnasingh4991
<script>// JavaScript implementation to find the// previous perfect square and cube// smaller than the given number // Function to find the previous// perfect square of the number Nfunction previousPerfectSquare(N){    let prevN = Math.floor(Math.sqrt(N));         // If N is already a perfect square    // decrease prevN by 1.    if (prevN * prevN == N)        prevN -= 1;     return prevN * prevN;} // Function to find the// previous perfect cubefunction previousPerfectCube(N){    let prevN = Math.floor(Math.cbrt(N));         // If N is already a perfect cube    // decrease prevN by 1.    if (prevN * prevN * prevN == N)        prevN -= 1;             return prevN * prevN * prevN;} // Driver Code    let n = 30;    document.write(previousPerfectSquare(n) + "<br>");    document.write(previousPerfectCube(n) + "<br>"); // This code is contributed by Manoj.</script>
25
27
 
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Rohit_ranjan
sapnasingh4991
Code_Mech
subhammahato348
mank1083
surindertarika1234
maths-perfect-cube
maths-perfect-square
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Find next greater number with same set of digits
Segment Tree | Set 1 (Sum of given range)
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java