Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Perfect Cube

Subject: Perfect Cube

From: julie86a KRATZ

Date: 13 Feb, 2009 02:03:02

Message: 1 of 8

Hello everybody!

How do I check if a number is a perfect cube?

I would be very glad to hear from you

Thanks

jk

Subject: Perfect Cube

From: Roger Stafford

Date: 13 Feb, 2009 02:22:01

Message: 2 of 8

"julie86a KRATZ" <juliekratz@gmx.net> wrote in message <gn2kcl$c3d$1@fred.mathworks.com>...
> Hello everybody!
>
> How do I check if a number is a perfect cube?
>
> I would be very glad to hear from you
>
> Thanks
>
> jk

  What do you have in mind when you say "a perfect cube"? Do mean that the number is an integer and has a cube root which is also an integer? The logical expression

 (sign(x)*round(abs(x)^(1/3)))^3==x

should be true if x is this kind of perfect cube. (Note: I have used the absolute value of x here to avoid complex roots.)

Roger Stafford

Subject: Perfect Cube

From: Husam Aldahiyat

Date: 13 Feb, 2009 05:18:01

Message: 3 of 8

"julie86a KRATZ" <juliekratz@gmx.net> wrote in message <gn2kcl$c3d$1@fred.mathworks.com>...
> Hello everybody!
>
> How do I check if a number is a perfect cube?
>
> I would be very glad to hear from you
>
> Thanks
>
> jk

if mod(a^(1/3),1)
s='no';
else
s='yes';
end

Subject: Perfect Cube

From: Matt Fig

Date: 13 Feb, 2009 05:37:01

Message: 4 of 8

"Husam Aldahiyat" <numandina@gmail.com> wrote in message
> if mod(a^(1/3),1)
> s='no';
> else
> s='yes';
> end

Husam,
There are problems with using mod for these kinds of things without a tolerance!


N = 20;
s = zeros(N,2);
for ii = 1:N;
    a = ii^3;
    if mod(a^(1/3),1)
        s(ii,2) = 0;
    else
        s(ii,2) = 1;
    end
    s(ii,1) = a;
end
s




8[T[ia_[Thy^ThXgTr\lraLXgbrVrU@brbrTX!XrgcUT\T`-rbZ_yV`3cbb

Subject: Perfect Cube

From: Roger Stafford

Date: 13 Feb, 2009 06:19:01

Message: 5 of 8

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <gn2vq9$c3b$1@fred.mathworks.com>...
> if mod(a^(1/3),1)
> s='no';
> else
> s='yes';
> end

  As Matt has shown, Husam, the test with a^(1/3) fails to work properly most of the time. The trouble is with the fraction 1/3 which cannot be expressed exactly. A 1/2 or 1/4 power seems to work all right as far as I have tried. Algorithms do exist that can find any n-th root exactly for a number that is the n-th power of an integer. I once found one in an ancient book on arithmetic that had belonged to my father.

Roger Stafford

Subject: Perfect Cube

From: Husam Aldahiyat

Date: 13 Feb, 2009 08:43:02

Message: 6 of 8

Ooops, I should've seen that coming. Thanks.

Subject: Perfect Cube

From: Loren Shure

Date: 13 Feb, 2009 12:39:37

Message: 7 of 8

In article <gn2kcl$c3d$1@fred.mathworks.com>, juliekratz@gmx.net says...
> Hello everybody!
>
> How do I check if a number is a perfect cube?
>
> I would be very glad to hear from you
>
> Thanks
>
> jk
>

nthroot can help you out

--
Loren
http://blogs.mathworks.com/loren

Subject: Perfect Cube

From: Steven Lord

Date: 13 Feb, 2009 14:31:43

Message: 8 of 8


"Husam Aldahiyat" <numandina@gmail.com> wrote in message
news:gn2vq9$c3b$1@fred.mathworks.com...
> "julie86a KRATZ" <juliekratz@gmx.net> wrote in message
> <gn2kcl$c3d$1@fred.mathworks.com>...
>> Hello everybody!
>>
>> How do I check if a number is a perfect cube?
>>
>> I would be very glad to hear from you
>>
>> Thanks
>>
>> jk
>
> if mod(a^(1/3),1)
> s='no';
> else
> s='yes';
> end

What does this code give you?

y = (-2)^3; % y is a perfect cube by definition
mod(y^(1/3), 1)

--
Steve Lord
slord@mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us