Previous perfect square and cube number smaller than number N | Function to find the previous perfect square of the number N ; If N is already a perfect square decrease prevN by 1. ; Function to find the previous perfect cube ; If N is already a perfect cube decrease prevN by 1. ; Driver Code
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 ) + " " ) ; document . write ( previousPerfectCube ( n ) + " " ) ;