MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Find count of binary number pattern within concatenated number

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91131] 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:38:31 -0400 (EDT)
  • References: <g7bqom$moc$1@smc.vnet.net> <489992F3.4070802@gmail.com>

On Wed, Aug 6, 2008 at 2:02 PM, Jean-Marc Gulliet
<jeanmarc.gulliet at gmail.com> wrote:
> 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}

Please, disregard this nonsense about the "Non-Overlapping"  function:
it is too restrictive and does not produce the desired result.

The first fucntion does work correctly -- at least as far as I can
tell. What I mean by overlapping is that for a sequence such as {1, 1,
0, 1, 1, 0, 1} the function will count 2 subsequences 1101 starting at
1 and 4, respectively. (Note that the beginning of the second sequence
is also the end of the first sequence.)

    StringPosition["1101101", "1101"]

    {{1, 4}, {4, 7}}

Another possible approach is to convert the numbers into strings and
use the string search functions.

    myCount[n_Integer] :=
     StringPosition[
       ToString[FromDigits[Flatten[IntegerDigits[Range[n], 2]]]],
       ToString[FromDigits[IntegerDigits[n, 2]]]][[All, 1]]

    myCount[13]

    {1, 12, 25, 38}

Sorry for the confusion,
-- Jean-Marc


  • Prev by Date: Re: No preferences for OSX
  • Next by Date: Re: Find count of binary number pattern within concatenated number
  • Previous by thread: Re: Find count of binary number pattern within concatenated number
  • Next by thread: Re: Find count of binary number pattern within concatenated number