I wrote the following code in order to compute combinations of n per k.Though it works right for number n between 1-30 i can't make it work for larger numbers.Does anybody have any idea on how to improve it?
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n,c,j,k;
cout<<"Give n:";cin>>n;
cout<<"Give k:";cin>>k;
/* i goes through all n-bit numbers */
for (int i=0; i<(1<<n); i++) {
/* masking the j'th bit as j goes through all the bits,
* count the number of 1 bits. this is called finding
* a population count.
*/
for (j=0,c=0; j<32; j++) if (i & (1<<j)) c++;
/* if that number is equal to k, print the combination... */
if (c == k) {
/* by again going through all the bits indices,
* printing only the ones with 1-bits
*/
for (j=0;j<32; j++) if (i & (1<<j))
cout<<j<<' ';
cout<<endl;
}
}
}
thanks.