Notifications
Clear all

[Closed] how to know integer is power of 1 or 2???

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.

4 Replies

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

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

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
 

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.