write a function to check if a number is a perfect cube in matlab

main.m
function result = isPerfectCube(number)
% This function takes a number as an argument and checks if it is a perfect cube.
% Returns true if the number is a perfect cube, false otherwise.

cubeRoot = round(number^(1/3)); % Find the cube root of the number and round to the nearest integer
if cubeRoot^3 == number % Check if cube of rounded cube root is equal to the number
    result = true; % If it is, then number is a perfect cube
else
    result = false; % Otherwise, number is not a perfect cube
end
end
508 chars
12 lines

To use this function in your program, save the code in a file named isPerfectCube.m in your Matlab working directory. Then, you can call the function with any number as an argument, like this:

main.m
result = isPerfectCube(27); % result = true
result = isPerfectCube(10); % result = false
89 chars
3 lines

gistlibby LogSnag