x exp 2

Discussion in 'Cadence' started by nour, Jun 12, 2008.

  1. nour

    nour Guest

    Hi everybody!

    Please how to check, by a skill fonction, that a number n could be
    written that way (x exp 2)?
     
    nour, Jun 12, 2008
    #1
  2. nour

    Riad KACED Guest

    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.
     
    Riad KACED, Jun 12, 2008
    #2
  3. nour

    Riad KACED Guest

    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, Jun 12, 2008
    #3
  4. Riad KACED wrote, on 06/12/08 23:20:
    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.
     
    Andrew Beckett, Jun 13, 2008
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
Loading...