Re: Find count of binary number pattern within concatenated number
- To: mathgroup at smc.vnet.net
- Subject: [mg91129] 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:08 -0400 (EDT)
- References: <g7bqom$moc$1@smc.vnet.net> <489992F3.4070802@gmail.com>
On Wed, Aug 6, 2008 at 2:54 PM, Diana Mecum <diana.mecum at gmail.com> wrote: > Jean, > > Thank you. A question. I count four non-overlapping repititions of the > pattern with n = 13 ? Correct. Indeed, the "Non-Overlapping" is too restrictive and does not produce the desired result. Please, just disregard it. What I mean by overlapping is that for a sequence such as {1, 1, 0, 1, 1, 0, 1} the function will count 2 subsequence 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} Regards, - Jean-Marc > Diana > > On Wed, Aug 6, 2008 at 5:02 AM, 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}