Re: Find count of binary number pattern within concatenated number
- To: mathgroup at smc.vnet.net
- Subject: [mg91153] Re: Find count of binary number pattern within concatenated number
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 7 Aug 2008 04:42:42 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g7bqom$moc$1@smc.vnet.net>
Diana wrote:
> Can someone tell me how to find the count of the occurrences of "1101"
> within "11011100101110111100010011010101111001101" generated with the
> FromDigits statements below? I will be increasing "n".
>
> n=13
>
> FromDigits[Flatten[IntegerDigits[Range[n],2]]]
>
> 11011100101110111100010011010101111001101.
>
> FromDigits[IntegerDigits[n, 2]]
>
> 1101
Here is two almost identical versions of the counting function: the
first one seeks for overlapping sequences, the second looks only for
non-overlapping sequences. Of course the results might be very different
for a same number.
(* Overlapping sequences *)
myCount[n_Integer] :=
Module[{nb2 = IntegerDigits[n, 2]},
Flatten[Position[
Partition[Flatten[IntegerDigits[Range[n], 2]], Length[nb2], 1],
nb2]]]
myCount[13]
{1, 12, 25, 38}
(* Non-Overlapping sequences *)
myCount[n_Integer] :=
Module[{nb2 = IntegerDigits[n, 2]},
Flatten[Position[
Partition[Flatten[IntegerDigits[Range[n], 2]], Length[nb2]],
nb2]]]
myCount[13]
{1, 7}
Regards,
-- Jean-Marc