//the following functions will count number of bits in the number int countbits(int n) { int count=0; while(n) { n/=2; count++; } return count; }
int countnumberof1(int number) { if(number==0) return 0; if(number==1) return 1; if(number==2) return 2; if(number==3) return 4; if(number>3) { int bits=countbits(number); return [(2^bits-1)+countnumberof1(2^bits-1)+countnumberof1(number-2^bits-1)]; } } On Tuesday, July 24, 2012 3:09:42 PM UTC+5:30, ruru wrote: > > find no. of 1's in binary format of numbers from 1 to 100. like for > 1 to 10 answer is 17 > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/IFbki8Z8tUgJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.