Previous perfect square and cube number smaller than number N | Python3 implementation to find the previous perfect square and cube smaller than the given number ; 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
import math NEW_LINE import numpy as np NEW_LINE def previousPerfectSquare ( N ) : NEW_LINE INDENT prevN = math . floor ( math . sqrt ( N ) ) ; NEW_LINE if ( prevN * prevN == N ) : NEW_LINE INDENT prevN -= 1 ; NEW_LINE DEDENT return prevN * prevN ; NEW_LINE DEDENT def previousPerfectCube ( N ) : NEW_LINE INDENT prevN = math . floor ( np . cbrt ( N ) ) ; NEW_LINE if ( prevN * prevN * prevN == N ) : NEW_LINE INDENT prevN -= 1 ; NEW_LINE DEDENT return prevN * prevN * prevN ; NEW_LINE DEDENT n = 30 ; NEW_LINE print ( previousPerfectSquare ( n ) ) ; NEW_LINE print ( previousPerfectCube ( n ) ) ; NEW_LINE