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 = 1
Input: 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++
#include <cmath>
#include <iostream>
using namespace std;
int previousPerfectSquare( int N)
{
int prevN = floor ( sqrt (N));
if (prevN * prevN == N)
prevN -= 1;
return prevN * prevN;
}
int previousPerfectCube( int N)
{
int prevN = floor (cbrt(N));
if (prevN * prevN * prevN == N)
prevN -= 1;
return prevN * prevN * prevN;
}
int main()
{
int n = 30;
cout << previousPerfectSquare(n) << "\n" ;
cout << previousPerfectCube(n) << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int previousPerfectSquare( int N)
{
int prevN = ( int )Math.floor(Math.sqrt(N));
if (prevN * prevN == N)
prevN -= 1 ;
return prevN * prevN;
}
static int previousPerfectCube( int N)
{
int prevN = ( int )Math.floor(Math.cbrt(N));
if (prevN * prevN * prevN == N)
prevN -= 1 ;
return prevN * prevN * prevN;
}
public static void main(String[] args)
{
int n = 30 ;
System.out.println(previousPerfectSquare(n));
System.out.println(previousPerfectCube(n));
}
}
|
Python3
import math
import numpy as np
def previousPerfectSquare(N):
prevN = math.floor(math.sqrt(N));
if (prevN * prevN = = N):
prevN - = 1 ;
return prevN * prevN;
def previousPerfectCube(N):
prevN = math.floor(np.cbrt(N));
if (prevN * prevN * prevN = = N):
prevN - = 1 ;
return prevN * prevN * prevN;
n = 30 ;
print (previousPerfectSquare(n));
print (previousPerfectCube(n));
|
C#
using System;
class GFG{
static int previousPerfectSquare( int N)
{
int prevN = ( int )Math.Floor(Math.Sqrt(N));
if (prevN * prevN == N)
prevN -= 1;
return prevN * prevN;
}
static int previousPerfectCube( int N)
{
int prevN = ( int )Math.Floor(Math.Cbrt(N));
if (prevN * prevN * prevN == N)
prevN -= 1;
return prevN * prevN * prevN;
}
public static void Main(String[] args)
{
int n = 30;
Console.WriteLine(previousPerfectSquare(n));
Console.WriteLine(previousPerfectCube(n));
}
}
|
Javascript
<script>
function previousPerfectSquare(N)
{
let prevN = Math.floor(Math.sqrt(N));
if (prevN * prevN == N)
prevN -= 1;
return prevN * prevN;
}
function previousPerfectCube(N)
{
let prevN = Math.floor(Math.cbrt(N));
if (prevN * prevN * prevN == N)
prevN -= 1;
return prevN * prevN * prevN;
}
let n = 30;
document.write(previousPerfectSquare(n) + "<br>" );
document.write(previousPerfectCube(n) + "<br>" );
</script>
|
Time Complexity: O(log(n))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
25 Sep, 2022
Like Article
Save Article