17
1
Inspired by the fourth problem from BMO2 2009.
Given a positive integer n as input or a parameter, return the number of positive integers whose binary representations occur as blocks in the binary expansion of n.
For example, 13 -> 6 because 13 in binary is 1101 and it has substrings 1101, 110, 101, 11, 10, 1
. We do not count binary numbers that start with zero and we do not count zero itself.
Test Cases
13 -> 6
2008 -> 39
63 -> 6
65 -> 7
850 -> 24
459 -> 23
716 -> 22
425 -> 20
327 -> 16
You may take in n as any of the following:
- an integer
- a list of truthy/falsy values for the binary representation
- a string for the binary representation
- a base 10 string (though I'm not sure why anyone would do this)
Make your code as short as possible.
3Can you confirm 63->5 and not 6? Bin(63)=111111 -> six distinct nonzero substrings – dylnan – 2018-01-22T17:19:56.343
Related. (Uses subsequences instead of substrings and doesn't disregard leading zeros.) – Martin Ender – 2018-01-22T17:22:01.363
1@dylnan Typo. Fixed. – 0WJYxW9FMN – 2018-01-22T17:24:42.837
@MartinEnder Is this different enough to stay on this site or shall I delete it as a duplicate? I think it's sufficiently different, but you know a lot better than I do. – 0WJYxW9FMN – 2018-01-22T17:26:55.223
@J843136028 The bigger difference for not making it a duplicate is the time restriction on the other challenge. You're fine. (Just posted the link, so that the challenges show up in each other's sidebar.) – Martin Ender – 2018-01-22T17:27:47.123