Find amount of numbers that are between 1 and 100 inclusive, when converted to binary have a digit sum of less than 5?

+1 vote
252 views
Find amount of numbers that are between 1 and 100 inclusive, when converted to binary have a digit sum of less than 5?
posted Jun 14, 2016

There are 87 decimal numbers from 1 to 100 (including 1 and 100)
That have their binary version's digits add upto 4 or as given in the question less than 5.

answer Jun 18, 2016
Here is the c code I used.
#include <stdio.h>
#include<math.h>
int main ()
{
int a,b,c,d,count;
count=0;
for (a=1;a<101;a++)
{
b=a;
d=0;
while (b>0)
{
c=b%2;
b=b/2;
d=d+c;
}
if (d<5)
{
count=count+1;
}
}

printf("There are %d many decimal numbers which have thier binary version's digits add upto or lesser than 5",count);

}
i have yet to study the listing but KUDOS

Similar Puzzles
+1 vote

Find the sum of all the prime numbers larger than 2 less than 10^12 that are 1 more than a perfect square. Because the number can get pretty big provide the answer mod 1007.

Note: Problem shouldn't take much more than one minute if your answer is taking too long consider looking for optimizations.