[algogeeks] Re: nth number of k bits

2009-09-10 Thread ashish gupta
I think this should be easy to understand. #includeiostream using namespace std; // this function generated next big number of the list having k bit set. unsigned next_number( unsigned x) { unsigned smallest, ripple, one; smallest = x (-x); ripple = x + smallest ; one = ripple^x;

[algogeeks] Re: nth number of k bits

2009-08-27 Thread Shishir Mittal
Its a bit similar to finding the rank of the word COMPUTER in the dictionary among the words formed from C,O,M,P,U,T,E,R. Find maximum r such that (k+r)C(r) n. This represents the total number of numbers formed from 'r' 0 bits and 'k' 1 bits. Since n is greater, it implies it has an extra 1 bit

[algogeeks] Re: nth number of k bits

2009-08-27 Thread Dave
Here is an algorithm (written in C) to find the n-th integer with k one bits... int NthIntWithKOneBits(int n, int k) { int i, iCk, result=0; if( n == 1 k == 0 ) return 0; if( n = 0 || k = 0 ) return -1; while( n 0 k 0 ) { i = k; iCk = 1; while(

[algogeeks] Re: nth number of k bits

2009-08-27 Thread Shishir Mittal
There were some errors in my solution. On Thu, Aug 27, 2009 at 6:10 PM, Shishir Mittal 1987.shis...@gmail.comwrote: Its a bit similar to finding the rank of the word COMPUTER in the dictionary among the words formed from C,O,M,P,U,T,E,R. Find maximum r such that (k+r)C(r) n. This

[algogeeks] Re: nth number of k bits

2009-08-27 Thread ankur aggarwal
@shishir plz give an example.. its bit tough to understand for me atleast.. On Thu, Aug 27, 2009 at 6:10 PM, Shishir Mittal 1987.shis...@gmail.comwrote: Its a bit similar to finding the rank of the word COMPUTER in the dictionary among the words formed from C,O,M,P,U,T,E,R. Find maximum r