PDA

View Full Version : how to know integer is power of 1 or 2???


luigi
06-21-2007, 04:41 PM
I been trying to know a way of.
i got and array and i want to know if my array is in power of 1 or power of two.

so 1,3,5 ,7 will be not in power of two
and 2,4,8.. will be power of two

im spanish my language we say par and impar dont know how to say in english.

i.ve been trying to find in the help but didnt got luck.

ypuech
06-21-2007, 04:49 PM
I think you can use modulo:

mod 7 2
1.0
mod 8 2
0.0
mod 16 2
0.0
mod 11 2
1.0
mod 1 2
1.0
When mod x 2 = 0 : x is power of two
When mod x 2 = 1 : x is not power of two

davestewart
06-21-2007, 05:00 PM
Hey,

In English we say:
"odd" : 1, 3, 5
"even" : 2, 4, 6

"Power of" would be:
"to the power of 2" or "squared" : 4 x 4 = 16
"to the power of 3" or "cubed" : 4 x 4 x 4 = 64

thatoneguy
06-21-2007, 05:37 PM
Another way to express it is mathematically:

(2n) = "Even" [2,4,6,8,...]
and
((2n) -1) = "Odd" [1,3,5,7,...]

anyway this should do it:



fn ParImpar n =


(local b
b = abs(mod n 2)if b == 0 then trueelse if b == 1 then false
else undefined
)

EstaImpar = ParImpar -1
EstaPar = ParImpar -2
EstaImpar = ParImpar 3
EstaPar = ParImpar 4
EstaNada = ParImpar 2.343

luigi
06-21-2007, 05:53 PM
wooo guys thanks for the quick reply.

is funny having to know al the names translation for matematical concepts.

ypuech thanks for the mod sample i thing i wil go with this.

CGTalk Moderation
06-21-2007, 05:53 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.