How to check whether a number is a perfect cube root in VB.NET

1 Answer

0 votes
' The cube root is a whole number. For example, 27 is a perfect cube, as ∛27 or (27)**1/3 = 3

Imports System

Public Class PerfectCubeRoot_VB_NET
    Public Shared Function isPerfectCubeRoot(ByVal x As Integer) As Boolean
        x = Math.Abs(x)
		
        Dim cubeRoot As Integer = CInt(Math.Round(Math.Pow(x, 1.0 / 3.0), MidpointRounding.AwayFromZero))
		
        Return Math.Pow(cubeRoot, 3) = x
    End Function

    Public Shared Sub Main(ByVal args As String())
        Console.WriteLine(isPerfectCubeRoot(16))
        Console.WriteLine(isPerfectCubeRoot(64))
        Console.WriteLine(isPerfectCubeRoot(27))
        Console.WriteLine(isPerfectCubeRoot(25))
        Console.WriteLine(isPerfectCubeRoot(-64))
        Console.WriteLine(isPerfectCubeRoot(-27))
        Console.WriteLine(isPerfectCubeRoot(729))
    End Sub
End Class



' run:
'
' False
' True
' True
' False
' True
' True
' True
'

 



answered Sep 1, 2024 by avibootz

Related questions

1 answer 138 views
1 answer 147 views
1 answer 177 views
1 answer 201 views
1 answer 195 views
1 answer 136 views
...