English 中文(简体)
2至2人
原标题:Perfect power of two between two numbers
  • 时间:2011-03-31 11:42:30
  •  标签:
  • algorithm

How can I find perfect power of two between two numbers? Sample input: 0 and 10 Output: 2, 4, 8

最佳回答

Well the interesting part is "How do I get the greatest power of 2 that is less than or equal to my upper bound" and the same for the lowest power of 2 that is greater or equal to the lower bound.

而且,这很容易在没有住所的情况下进行。 未签署32个借项号:

floor(x):   ; floor power of 2
    x = x | (x >> 1)
    x = x | (x >> 2)
    x = x | (x >> 4)
    x = x | (x >> 8)
    x = x | (x >> 16)
    return x - (x >> 1)

ceil(x):   ; ceiling power of 2
    x = x - 1
    x = x | (x >> 1)
    x = x | (x >> 2)
    x = x | (x >> 4)
    x = x | (x >> 8)
    x = x | (x >> 16)
    return x + 1

你们获得了生产数字的通道,但 well得好。

问题回答

选择第一组最高点为1级,即从最低点算起。 之后,第二位数中位数为一,即处于正轨状态。 2x+1, 2x+2......, 2y 是你重新寻找的人数。

您可使用数字和产出的双位代表,在只设定一个比值的中间点之间:

0  = 00000000
10 = 00001010

=>

     00000001 (1)
     00000010 (2)
     00000100 (4)
     00001000 (8)

因此,你的问题被缩小,以找到比最低点更长的头一种权力,然后在你比最高点更低时转移。 另一种做法是,除最高值外,将所有设定的限额固定在最高值之内,然后在你超过最低值时转移权利。

How many times can you bit-shift before getting to 0?

步骤:

  1. Let s say n1 = start_of_range, n2 = end_of_range.
  2. Find out how many bits are needed to represent n1. Call it b.
  3. Now 2**b will be the next power of two after n1.
  4. It should be easy to calculate all the power of twos till n2 from this.

Sample python Code:

#!/usr/bin/python
def get_bits(n):
    b = 0
    while n:
        n = n / 2
        b += 1
    return b

def get_power_of_two_in_range(n1, n2):
    b = get_bits(n1)
    x = 2**b
    res = []
    while x < n2:
        res.append(x)
        x = x * 2
    return res

def main():
    print get_power_of_two_in_range(0, 10)

if __name__ ==  __main__ :
    main()
int first=0; //**first number which is a complete power of 2 in the range!**
 for(i=low;i<high;i++){
  if(2i==(i^(i-1)+1)){
   first=i;
   break;
  }
 }

while(first!=0){
 print first;
 first*=2;
 if(first>high){
  break;
}
}




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签