I got 682,560 as well.
@xgeutzu: Your binary_counting function is way too complicated for what it does, but I think it works. So, assuming that it works, the rest should be as simple as this (brute force):
[pseudocode]
numberOfOnes = 0
for(hour = 0 -> 23)
for(minute = 0 -> 59)
for(second = 0 -> 59)
numberOfOnes += binary_counting(hour)
numberOfOnes += binary_counting(minute)
numberOfOnes += binary_counting(second) |
Optimization suggestions
If you need it to go faster, just calculate and cache the results of binary_counting on numbers 0-59, since those are the only ones you ever use:
1 2 3 4 5 6 7 8
|
//cache off the range of relevant values instead of recalculating everytime
int count_of_ones[60];
for(int i = 0; i < 60; ++i)
{
count_of_ones[i] = binary_counting(i);
}
//...
numberOfOnes += count_of_ones[hour];
|
You can also calculate the sum of number of ones that each hour contributes, then multiply that value by 3600, since each hour is shown in 3600 unique time slots. (each hour number is shown for 60 minutes, 60 seconds in each of those minutes, 60*60 = 3600) Recall that
count_of_ones[0] * 3600
+ count_of_ones[1] * 3600
+ count_of_ones[2] * 3600
+...
+ count_of_ones[23] * 3600
|
equals
(count_of_ones[0] + ... + count_of_ones[23]) * 3600 |
so you only have to do the multiplication once.
The same optimization can be done for minutes and seconds. However, each minute number is shown 1440 unique times. (Any given minute number is shown once per hour, for a duration of 60 seconds, 24*60 = 1440.) The same applies to seconds.
Calculating the contribution of ones by each hour number, minute number, and second number like this reduces the amount of looping you'd otherwise have to perform.
Hold off on optimizing until you're sure you've reached the correct solution with the straight-forward, brute-force method.