x exp 2

N

nour

Guest
Hi everybody!

Please how to check, by a skill fonction, that a number n could be
written that way (x exp 2)?
 
Hi Nour !

This bit is off the top of my head
;;
procedure( rkIsIntegerPowerOfX(n p)
let(((counter 0) (a n) (b n))
if(onep(b)
t
if(zerop(modulo(a p))
then
b=a/p
counter++
rkIsIntegerPowerOfX(b p)
else
nil
)
)
)
)
;;

It is a recursive function that returns t/nil whether N is a power of
P. You can try :
ciw> rkIsIntegerPowerOfX(1 2)
--> t
ciw> rkIsIntegerPowerOfX(2 2)
--> t
ciw> rkIsIntegerPowerOfX(3 2)
--> nil
.......

I used '2' as an example but it could take any other integer. n=0
would come with a stack overflow !!
You may then add some custom skill on top of this function to check
'n' is a non-zero positive integer (skill functions zerop/plusp/
integerp ...) and any other thing you made need to print. Don't
hesitate to come back should you need any further help.
Please do note that the max skill integer (as far as I know) is
n=2**31-1 =2147483647

Take care,
Riad.
 
Hi Nour !
This bit is off the top of my head
;;
procedure( rkIsNPowerOfP(n p)
let(((a n) (b n))
if(onep(b)
t
if(zerop(modulo(a p))
then
b=a/p
rkIsNPowerOfP(b p)
else
nil
)
)
)
)
;;
It is a recursive function that returns t/nil whether N is a power of
P. You can try :
ciw> rkIsIntegerPowerOfX(1 2)
--> t
ciw> rkIsIntegerPowerOfX(2 2)
--> t
ciw> rkIsIntegerPowerOfX(3 2)
--> nil
.......
I used '2' as an example but it could take any other integer. n=0
would come with a stack overflow !!
You may then add some custom skill on top of this function to check
'n' is a non-zero positive integer (skill functions zerop/plusp/
integerp ...) and any other thing you made need to print. Don't
hesitate to come back should you need any further help.
Please do note that the max skill integer (as far as I know) is
n=2**31-1 =2147483647
Take care,
Riad.
 
Riad KACED wrote, on 06/12/08 23:20:
Hi Nour !
This bit is off the top of my head
;;
procedure( rkIsNPowerOfP(n p)
let(((a n) (b n))
if(onep(b)
t
if(zerop(modulo(a p))
then
b=a/p
rkIsNPowerOfP(b p)
else
nil
)
)
)
)
;;
It is a recursive function that returns t/nil whether N is a power of
P. You can try :
ciw> rkIsIntegerPowerOfX(1 2)
--> t
ciw> rkIsIntegerPowerOfX(2 2)
--> t
ciw> rkIsIntegerPowerOfX(3 2)
--> nil
......
I used '2' as an example but it could take any other integer. n=0
would come with a stack overflow !!
You may then add some custom skill on top of this function to check
'n' is a non-zero positive integer (skill functions zerop/plusp/
integerp ...) and any other thing you made need to print. Don't
hesitate to come back should you need any further help.
Please do note that the max skill integer (as far as I know) is
n=2**31-1 =2147483647
Take care,
Riad.
Could also do:

defun(abIsPowerOfX (n x)
power=log(n)/log(x)
abs(power-round(power))<1e-6*abs(power)
)

Or:

defun(abIsPowerOf2 (n "x")
let(((highCount 0) (bit 0))
while(highCount<=1 && bit<=31
highCount=highCount+n<bit>
bit++
)
highCount<=1
)
)

The second uses bitfields, and only works with integers.

Andrew.
 

Welcome to EDABoard.com

Sponsor

Back
Top