I think the question is in the written round!!! On Tue, Jul 24, 2012 at 9:11 PM, algo bard <algo.b...@gmail.com> wrote:
> #include<stdio.h> > #define RANGE_START 0 > #define RANGE_END 100 > > int main() > { > int i,n,ctr=0; > > for( i=RANGE_START ; i<=RANGE_END ; i++) > { > n = i; > while(n) // Using Brian Kernighan's Algorithm to count the number > of set bits in a number. > { > n= n&n-1; > ctr++; > } > > } > > printf("%d ",ctr); > } > > TC = No. of set bits in the given range of numbers. > > > On Tue, Jul 24, 2012 at 7:56 PM, Lomash Goyal <lomesh.go...@gmail.com>wrote: > >> // count number of 1's upto n.cpp : Defines the entry point for the >> console application. >> // >> >> #include "stdafx.h" >> #include<math.h> >> #include<conio.h> >> //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); >> if(number==pow(2.0,bits)-1) >> { >> return pow(2.0,bits-1)+2*countnumberof1(pow(2.0,bits-1)-1); >> } >> else return >> pow(2.0,bits-2)+2*countnumberof1(pow(2.0,bits-2)-1)+countnumberof1(number-(pow(2.0,bits-1)))+number-(pow(2.0,bits-1))+1; >> } >> } >> int _tmain(int argc, _TCHAR* argv[]) >> { >> printf("%d",countnumberof1(10)); >> getch(); >> return 0; >> } >> >> >> On Tue, Jul 24, 2012 at 3:09 PM, ruru <soupti...@gmail.com> 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 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. >>> >>> >> >> >> -- >> Regards >> >> Lomash Goyal >> >> * >> * >> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> 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. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > 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. > -- With regards, Manish kumar untwal Indian Institute of Information Technology Allahabad (2009-2013 batch) -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. 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.