The Exact Number of Squares in Fibonacci Words Aviezri S. Fraenkel1 and Jamie Simpson2 Abstract. All our words (sequences) are binary. A square is a subword of the form uu (concatenation). Two squares are distinct if they are of different shape, not just translates of each other. Otherwise they are repeated. Fibonacci words are defined by f0 = 0, f1 = 1, fm = fm\Gamma 1fm\Gamma 2 for m * 2. Let Fm = jfmj. Then F0 = 1, F1 = 1, Fm = Fm\Gamma 1 + Fm\Gamma 2 (m * 2) are the Fibonacci numbers. Let D(n) and R(n) be the exact number of Distinct and Repeated squares respectively in fn. We prove: D(n) = 2(Fn\Gamma 2 \Gamma 1) (n * 5), which implies, asymptotically, D(n) = 2(2 \Gamma ')Fn + o(1) (2(2 \Gamma ') ss 0:7639), where ' is the golden section. We also prove: R(n) = 45nFn \Gamma 25(n + 6)Fn\Gamma 1 \Gamma Fn\Gamma 2 + n + 1 (n * 3). This yields R(n) = 25(3\Gamma ')nFn+O(Fn) = 2(3\Gamma ')5 log 2 ' Fn log2 Fn+O(Fn) i 2(3\Gamma ') 5 log2 ' ss 0:7962j. 1. Introduction We consider binary words (sequences) throughout. A square in a word is a factor (subword) of the form u2, i.e., the concatenation of two identical subwords. Two squares are distinct if they are of different shape, not just translates of each other. Two squares which are not distinct, beginning at two different locations of a word, are repeated squares. A word u is primitive if u cannot be written in the form u = vj for some integer j * 2. Otherwise u is nonprimitive. A square u2 with u primitive will be called a primitive rooted square. Fibonacci words are defined by f0 = 0, f1 = 1, fm = fm\Gamma 1fm\Gamma 2 (concatenation) for all m * 2. Let Fm = jfmj. Then F0 = 1, F1 = 1, Fm = Fm\Gamma 1 + Fm\Gamma 2 (m * 2) are the Fibonacci numbers, since jfmj = jfm\Gamma 1fm\Gamma 2j = jfm\Gamma 1j + jfm\Gamma 2j. The first 10 Fibonacci words are depicted in Table 1 in x2. Fibonacci words have been studied extensively. (The query Fibonacci And word*, run on MathSciNet on April 2, 1997, yielded 86 papers.) The infinite Fibonacci word is the word which contains fm as a prefix for every m * 1. For the study of repetitions in Fibonacci words, mainly in the infinite Fibonacci word, see e.g., [dLV1990], [MiP1992], [IMS*1997]. Incidentally, all squares in fm (m * 0) are primitive rooted (see e.g., [CrR1994], Ch. 2). Denote by D(n) and by R(n) the exact number of Distinct and Repeated squares respectively in the Fibonacci word fn. In x2 we prove, 1Department of Applied Mathematics and Computer Science, Weizmann Institute of Science, Rehovot 76100, Israel; http://www.wisdom.weizmann.ac.il/~fraenkel fraenkel@wisdom.weizmann.ac.il 2School of Mathematics, Curtin University, Perth WA 6001, Australia; http://www.cs.curtin.edu.au/~simpson simpson@cs.curtin.edu.au 1 2 Theorem 1. For n * 5, D(n) = 2(Fn\Gamma 2 \Gamma 1). We mention in passing that in [Sto1976] and [FMT1978], the multitude of squares in Fibonacci words and generalizations thereof is viewed from a slightly different, yet related point of view. As customary, denote the roots of x2 \Gamma x\Gamma 1 = 0 by ' = 1+ p5 2 (the golden section) and ' = 1\Gamma p5 2 .Then ' + ' = 1, ' \Gamma ' = p5, and '' = \Gamma 1, '2 = ' + 1, '2 = ' + 1. Denote by N (a) the nearest integer to a. Theorem 1 leads to the following asymptotic result for D(n). Corollary 1. We have D(n) = 2(2 \Gamma ')Fn + o(1) (2(2 \Gamma ') ss 0:7639); and also, for n * 5, D(n) = N ` 2' n\Gamma 1p 5 \Gamma 2' : Fibonacci words have been used for giving lower bounds on the number of repeated primitive rooted squares. We are unaware of previous results for D(n), but estimates for R(n) were given previously. In [Cro1981], Lemma 10, it was shown that R(n) * 16 Fn log2 Fn for n * 5. Theorem 11 in [CrR1995] says that the number of primitive rooted square prefixes of a word w is ! logOE jwj. For w = fn this implies R(n) ! Fn logOE Fn = Fn log2 Fn= log2 OE ^ 1:441Fn log2 Fn. In [IMS*1997] the structure of squares in Fibonacci words, especially infinite Fibonacci words, was studied. Rather than just estimating the number of squares in a finite Fibonacci word, as done previously, we give an exact formula for it. Theorem 2. The function R(n) is given by R(n) = 45nFn \Gamma 25 (n + 6)Fn\Gamma 1 \Gamma Fn\Gamma 2 + n + 1 for n * 3. Theorem 2 improves the above two results [Cro1981], [CrR1995]. Asymptotically it gives, Corollary 2. We have R(n) = 25 (3 \Gamma ')nFn + O(Fn) = 2(3 \Gamma ')5 log 2 ' F n log2 Fn + O(Fn) ` 2(3 \Gamma ')5 log 2 ' ss 0:7962' : Comparing this with Corollary 1, we see that the number of repeated squares is logarithmically larger than the number of distinct squares in fn with almost the same numerical multiplicative constant. The maximum number M (n) of distinct squares a binary word of length n can have, is larger than that found in Fibonacci words: in [FrS*1997] we proved that M (n) ! 2n for all positive integers n, and constructed a special set of words to show that M (n) = n \Gamma o(n) for infinitely many n. These two results hold for both nonprimitive and primitive rooted squares. 2. Distinct Squares in Fibonacci Words We use several lemmas to establish Theorem 1. The Fibonacci words fn were defined in the Introduction. The first few are listed in Table 1. 3 Table 1. f0 = 0 f1 = 1 f2 = 10 f3 = 101 f4 = 10110 f5 = 10110101 f6 = 1011010110110 f7 = 101101011011010110101 f8 = 1011010110110101101011011010110110 f9 = 1011010110110101101011011010110110101101011011010110101 Lemma 1. For m * 5, fm contains f2m\Gamma 4; and for m * 7, fm contains f3m\Gamma 4. Proof. fm = fm\Gamma 1fm\Gamma 2 = fm\Gamma 2fm\Gamma 3fm\Gamma 2 = fm\Gamma 3f2m\Gamma 4fm\Gamma 5fm\Gamma 2: The suffix fm\Gamma 2 of this formula can be iterated to yield fm\Gamma 2 = fm\Gamma 6fm\Gamma 7fm\Gamma 6fm\Gamma 5fm\Gamma 4, so fm = fm\Gamma 3f3m\Gamma 4fm\Gamma 7fm\Gamma 6fm\Gamma 5fm\Gamma 4. \Xi Definition 1. For any word x = x1 : : : xn and any j 2 f0; : : : ; n \Gamma 1g, a word of the form xj+1 : : : xnx1 : : : xj is called a conjugate or a rotation of degree j of x. A word of length n has n conjugates, including itself, which is a conjugate of degree 0. Lemma 2. Let u = xxy for subwords x, y with jyj ! jxj. Let i be any integer satisfying 0 ^ i ^ jyj. Then u contains a square v2i+1 of length 2jxj, beginning at position i + 1 of u, where vi+1 is a conjugate of degree i of x, if and only if the prefix of x of length i is identical to the prefix of y of length i. Proof. See Fig. 1. Since i ^ jyj ! jxj, v2i+1 is a square of length 2jxj beginning at position i + 1 of u if and only if the prefix of length i + 1 of the second occurrence of x = suffix of length i + 1 of the first occurrence of vi+1 = suffix of length i + 1 of the second occurrence of vi+1 = prefix of length i + 1 of y. If the condition is satisfied, it's clear that vi+1 is a conjugate of degree i of x. \Xi x x vi+1 vi+1 i i y i+1 i Figure 1. 4 Remark. Lemma 2 is true also for jxj = jyj. We excluded that case since for i = jxj = jyj, the square v2i+1 is x2, which was already counted for i = 0. It will become clear from Lemma 8 below that, when u is a Fibonacci word, the squares v2i+1 are distinct in pairs for i 2 f0; : : : jxj \Gamma 1g. Notation. Let u, v be factors (subwords) of a word w such that w = uv. Then wv\Gamma 1 denotes the prefix u of w with its suffix v deleted. Thus u = wv\Gamma 1. By si(m) we denote the suffix of length i of fm, and by si(m) its complement. For example, f4 = 10110, so f4s2(4)\Gamma 1s2(4) = 10101. Lemma 3. For m * 5 and i 2 f1; : : :; m \Gamma 4g, all the Fi conjugates u of fi have the property that fm contains u2. Proof. Since f1 = 1; f2 = 10 and 12; (10)2; (01)2 appear in fm for every m * 5, the result holds for i 2 f1; 2g. We may thus assume i * 3 (whence m * 7). Clearly fm contains fi+4 as a subword. Now i + 4 * 7, so by Lemma 1, fi+4 contains f3i , hence fm contains f3i . In particular, fm contains xxy with x = fi and y = fis1(i)\Gamma 1. The result now follows from Lemma 2. \Xi Lemma 4. Let k, m be integers with k * 3. For m * 2k + 2, we have (1) fm = f2m\Gamma 3fm\Gamma 4f\Gamma 1m\Gamma 2kfm\Gamma 2k\Gamma 2fm\Gamma 2k\Gamma 1fm\Gamma 2: For m * 2k + 1, (2) fm = f2m\Gamma 2fm\Gamma 3f\Gamma 1m\Gamma 2k+1fm\Gamma 2k\Gamma 1fm\Gamma 2k: Proof. We remark that for the words (1) and (2) to even exist, fm\Gamma 2k has to be a suffix of fm\Gamma 4, and fm\Gamma 2k+1 has to be a suffix of fm\Gamma 3. In fact, fm\Gamma 2k is trivially a suffix of fm\Gamma 4 for k = 2. If it holds for k, then it holds also for k + 1, since fm\Gamma 2k = fm\Gamma 2k\Gamma 1fm\Gamma 2k\Gamma 2. The same argument shows that fm\Gamma 2k+1 is a suffix of fm\Gamma 3. For proving (2) we write, for k * 3, (3) fm = f2m\Gamma 2fm\Gamma 4fm\Gamma 6 : : : fm\Gamma 2k+2fm\Gamma 2k\Gamma 1fm\Gamma 2k; whose validity we prove by induction on k: fm = fm\Gamma 1fm\Gamma 2 = fm\Gamma 2fm\Gamma 3fm\Gamma 3fm\Gamma 4 = fm\Gamma 2fm\Gamma 3fm\Gamma 4fm\Gamma 5fm\Gamma 5fm\Gamma 6 = f2m\Gamma 2fm\Gamma 5fm\Gamma 6fm\Gamma 7fm\Gamma 6 = f2m\Gamma 2fm\Gamma 4fm\Gamma 7fm\Gamma 6; so (3) holds for k = 3. Assume it holds for k. From fm\Gamma 2k\Gamma 1fm\Gamma 2k = fm\Gamma 2k\Gamma 1fm\Gamma 2k\Gamma 1fm\Gamma 2k\Gamma 2 = fm\Gamma 2k\Gamma 1fm\Gamma 2k\Gamma 2fm\Gamma 2k\Gamma 3fm\Gamma 2k\Gamma 2 = fm\Gamma 2kfm\Gamma 2k\Gamma 3fm\Gamma 2k\Gamma 2; (with m * 2(k + 1) + 1) we see that it also holds for k + 1, establishing (3). We use (3) to write fm = f2m\Gamma 2fm\Gamma 4fm\Gamma 6 : : : fm\Gamma 2k+2fm\Gamma 2k+1f\Gamma 1m\Gamma 2k+1fm\Gamma 2k\Gamma 1fm\Gamma 2k: Now fm\Gamma 4fm\Gamma 6 : : : fm\Gamma 2k+2fm\Gamma 2k+1 = fm\Gamma 3, by "collapsing" fm\Gamma 2k+3 = fm\Gamma 2k+2fm\Gamma 2k+1 all the way through till fm\Gamma 3 = fm\Gamma 4fm\Gamma 5. This proves (2). Finally, replace m by m \Gamma 1 in (2), and concatenate fm\Gamma 2 to the right end of both sides of the resulting identity, and then use fm = fm\Gamma 1fm\Gamma 2 for the left side. This gives (1). \Xi 5 Lemma 5. (a) For m * 6, fm = f2m\Gamma 3fm\Gamma 4s2(m \Gamma 4)\Gamma 1s2(m \Gamma 4)fm\Gamma 2. (b) For m * 5, fm = f2m\Gamma 2fm\Gamma 3s2(m \Gamma 3)\Gamma 1s2(m \Gamma 3). Proof. The assertion (b) is seen to hold for m = 5 and m = 6 (Table 1). Thus we may assume m * 7. Suppose first that m is odd. Then m = 2j + 1 for some j * 3. Since Lemma 4 holds for all k * 3, we may substitute k = j = (m \Gamma 1)=2 into (2), to get fm = f2m\Gamma 2fm\Gamma 3f\Gamma 12 f0f1 = f2m\Gamma 2fm\Gamma 3(10)\Gamma 101 = f2m\Gamma 2fm\Gamma 3s2(m \Gamma 3)\Gamma 1s2(m \Gamma 3) as asserted. Secondly, let m be even. Then m = 2j + 2 with j * 3. Substituting k = j = (m \Gamma 2)=2 into (2), we get fm = f2m\Gamma 2fm\Gamma 3f\Gamma 13 f1f2 = f2m\Gamma 2fm\Gamma 3(101)\Gamma 1110 = f2m\Gamma 2fm\Gamma 3(01)\Gamma 110 = f2m\Gamma 2fm\Gamma 3s2(m \Gamma 3)\Gamma 1s2(m \Gamma 3). For proving (a) we again replace m by m \Gamma 1 in (b), followed by concatenating fm\Gamma 2 to the right of both sides. \Xi Lemma 6. For m * 6, there are precisely Fm\Gamma 4+1 conjugates u of fm\Gamma 3, of degrees 0; : : : ; Fm\Gamma 4, such that u2 is in fm. Proof. We scan fm, from left to right, in a search for all the conjugates u of fm\Gamma 3 such that u2 is in fm. For m * 5, fm\Gamma 4 is a prefix of fm\Gamma 3 = fm\Gamma 4fm\Gamma 5. By Lemma 5 (a), we can thus apply Lemma 2 with x = fm\Gamma 3 and y = fm\Gamma 4s2(m \Gamma 4)\Gamma 1s2(m \Gamma 4), to conclude that for m * 6, fm contains Fm\Gamma 4 \Gamma 1 conjugates u of fm\Gamma 3, of degrees 0; : : :; Fm\Gamma 4 \Gamma 2, such that u2 is in fm. These squares begin at positions 1; : : : ; Fm\Gamma 4 \Gamma 1 of fm. By Lemma 2, the words of length 2Fm\Gamma 3 beginning at the positions in the closed interval [Fm\Gamma 4; Fm\Gamma 4 + Fm\Gamma 3] = [Fm\Gamma 4; Fm\Gamma 2] are not squares, since the second half of each such word contains, by Lemma 5 (a), either a bit at position 2Fm\Gamma 3 + Fm\Gamma 4 \Gamma 1 = Fm\Gamma 1 \Gamma 1 or at position Fm\Gamma 1, whose complements appear at positions Fm\Gamma 1 \Gamma 1 \Gamma Fm\Gamma 3 = Fm\Gamma 2 \Gamma 1 and Fm\Gamma 2 in the first half. We are now up to position Fm\Gamma 2 + 1. For m * 4, fm = fm\Gamma 1fm\Gamma 2 = fm\Gamma 2f2m\Gamma 3fm\Gamma 4. From Lemma 2 with x = fm\Gamma 3, y = fm\Gamma 4, we thus deduce that fm contains the first Fm\Gamma 4 + 1 conjugates u of fm\Gamma 3 such that u2 is in fm. These squares begin at Fm\Gamma 2 + 1; : : : ; Fm\Gamma 2 + Fm\Gamma 4 + 1. The last of these ends at position Fm\Gamma 2 + Fm\Gamma 4 + 2Fm\Gamma 3 = Fm. We have thus completed our scan, which yielded Fm\Gamma 4+1 distinct squares of length 2Fm\Gamma 3. The conjugates of fm\Gamma 3 of degrees 0; : : :; Fm\Gamma 4\Gamma 2 appeared twice each in these squares, and the conjugates of degrees Fm\Gamma 4 \Gamma 1 and Fm\Gamma 4 appeared once each. \Xi Lemma 7. For m * 5, there are precisely Fm\Gamma 3\Gamma 1 conjugates u of fm\Gamma 2, of degrees 0; : : : ; Fm\Gamma 3\Gamma 2, such that u2 belongs to fm. Proof. The proof is similar to that of Lemma 6, but simpler. For m * 4, fm\Gamma 3 is a prefix of fm\Gamma 2 = fm\Gamma 3fm\Gamma 4. By Lemma 5 (b), we can thus apply Lemma 2 with x = fm\Gamma 2, y = fm\Gamma 3s2(m \Gamma 3)\Gamma 1s2(m \Gamma 3), to conclude that for m * 5, fm contains Fm\Gamma 3 \Gamma 1 conjugates u of fm\Gamma 2, of degrees 0; : : :; Fm\Gamma 3 \Gamma 2, such that u2 is in fm. The last of those begins at position Fm\Gamma 3 \Gamma 1 and ends at position (Fm\Gamma 3 \Gamma 1) + (2Fm\Gamma 2 \Gamma 1) = Fm \Gamma 2 of fm. Thus there are only two remaining words of length 2Fm\Gamma 2 in fm. By Lemma 2 these could be squares only if the penultimate bit of fm were the same as the penultimate bit of fm\Gamma 3; but it is not by Lemma 5 (b). \Xi 6 Definition 2. A border of a word x is a subword which is both a prefix and suffix of x. Denote by bm the longest border of fm, and let Bm = jbmj. For example, b5 = 101, so B5 = 3. In Lemma 2.5 of [IMS*1997] it is proved that Bm = Fm\Gamma 2. (Actually their method shows that even bm = fm\Gamma 2.) Their Lemma 2.6 states that every conjugate of fm of positive degree is different from fm. We need the following generalization of this result: Lemma 8. For every integer m * 0, all the conjugates of fm are distinct in pairs. Proof. The result holds for m ^ 2, so we may assume m * 3. Let fm = x1 : : : xk (k = Fm). Suppose that xi+1 : : : xkx1 : : : xi = xj+1 : : : xkx1 : : : xj for some i; j 2 f0; : : :; k \Gamma 1g with i ! j. Removing the suffix of length i and concatenating it to the beginning, we get fm = x1 : : : xk = xj\Gamma i+1 : : : xkx1 : : : xj\Gamma i. Thus xj\Gamma i+1 : : : xk and x1 : : : xj\Gamma i are both borders of fm and one of them has length at least Fm=2 ? Fm\Gamma 2, contradicting the fact that Bm = Fm\Gamma 2. \Xi Proof of Theorem 1. Table 2 shows that the assertion holds for the first few values of n (but not for n = 4). Assume n * 7; then all the lemmas of this section can be applied. Table 2. n Fn jD(n)j 4 5 1 5 8 4 6 13 8 7 21 14 8 34 24 By Lemma 3, for i 2 f1; : : :; n \Gamma 4g, the maximum number Fi of conjugates u of fi have the property that u2 is in fn. By Lemma 6, precisely Fn\Gamma 4 + 1 conjugates u of fn\Gamma 3 have the property that u2 is in fn. By Lemma 7, precisely Fn\Gamma 3 \Gamma 1 conjugates u of fn\Gamma 2 have the property that u2 is in fn. Thus, D(n) * n\Gamma 4X i=1 Fi + (Fn\Gamma 4 + Fn\Gamma 3) = (Fn\Gamma 2 \Gamma 2) + Fn\Gamma 2 = 2(Fn\Gamma 2 \Gamma 1): No conjugate of fn\Gamma 1 can be a square in fn, since 2Fn\Gamma 1 ? Fn. On the other hand, there is the following "folklore" result, proved in [S'e'e1985] (see Theorem 2.3 of [IMS*1997]): For every nonempty subword u2 of fn, u is a conjugate of fm for some m * 1. It follows that the last inequality is actually an equality. \Xi Proof of Corollary 1. From Theorem 1, D(n) = 2(Fn\Gamma 2 \Gamma 1) = 2'\Gamma 2Fn + o(1) = 2(2 \Gamma ')Fn + o(1). The second part follows from the well-known identity Fn = ' n+1 \Gamma ' n+1p 5 (n * 0): \Xi 7 In fact, D(Fn) * 0:75Fn for n * 11. 3. The Exact Number of Repeated Squares in Fibonacci Words The first few values of R(n), the number of repeated squares in fn, found by computer, are listed in Table 3. Table 3. n R(n) 0,1,2,3 0 4 1 5 4 6 11 7 26 8 57 9 118 10 235 11 454 12 857 13 1588 14 2899 Since fm = fm\Gamma 1fm\Gamma 2, repeated squares in fm can be of three kinds. First, they can lie entirely within the first Fm\Gamma 1 bits of fm; there are clearly R(m \Gamma 1) such repeated squares. Secondly, they can lie entirely within the last Fm\Gamma 2 bits of fm; there are R(m \Gamma 2) such repeated squares. Thirdly, they can intersect both the first Fm\Gamma 1 bits and the last Fm\Gamma 2 bits of fm. This third kind of repeated squares will be called boundary squares of fm, since they cross the boundary between fm\Gamma 1 and fm\Gamma 2, and fm = fm\Gamma 1fm\Gamma 2. Their number is denoted by B(m). Thus, (4) R(m) = R(m \Gamma 1) + R(m \Gamma 2) + B(m): We will get a recurrence relation for R(m) by obtaining an expression for B(m). To do so, we partition the boundary squares of fm into two subsets. Boundary squares of type 1. For m * 5 we have, (5) fm = fm\Gamma 1jfm\Gamma 2 = fm\Gamma 2fm\Gamma 3jfm\Gamma 3fm\Gamma 4 = fm\Gamma 2fm\Gamma 3jfm\Gamma 4fm\Gamma 5fm\Gamma 4; where the boundary is indicated by a vertical bar. We see that every boundary square of fm\Gamma 2 = fm\Gamma 3fm\Gamma 4 is also a boundary square of fm. A boundary square of type 1 is a boundary square of fm\Gamma 2, which straddles the boundary of fm. In 8 other words, it is a boundary square which is contained in the copy of fm\Gamma 2 which straddles the boundary of fm. The number B1 of such squares is B(m \Gamma 2), which, by (4), is (6) B1 = B(m \Gamma 2) = R(m \Gamma 2) \Gamma R(m \Gamma 3) \Gamma R(m \Gamma 4): Boundary squares of type 2. A boundary square of type 2 is a boundary square which does not lie inside the copy of fm\Gamma 2 which straddles the boundary of fm. Clearly, every boundary square is either of type 1 or of type 2. In Lemmas 9-11 we count the number B2 of boundary squares of type 2. Lemma 9. For m * 8, fm contains no boundary squares of type 2 of length 2Fi for i ^ m \Gamma 6. There are Fm\Gamma 7 \Gamma 1 boundary squares of type 2 of length 2Fm\Gamma 5. Proof. For proving the first part, note that if a square of length 2Fi for i ^ m\Gamma 6 would straddle the boundary of fm, it would be contained in fm\Gamma 2, since 2Fm\Gamma 6 ! Fm\Gamma 4 and fm\Gamma 2 = fm\Gamma 3jfm\Gamma 4, so it would be of type 1, not type 2. By Lemma 3, fm contains * Fm\Gamma 5 repeated squares of length 2Fm\Gamma 5. From (5), (7) fm = fm\Gamma 2fm\Gamma 3jfm\Gamma 4fm\Gamma 5fm\Gamma 4 = fm\Gamma 2fm\Gamma 4fm\Gamma 5jfm\Gamma 5fm\Gamma 6fm\Gamma 5fm\Gamma 4 = fm\Gamma 2fm\Gamma 4fm\Gamma 5jfm\Gamma 5fm\Gamma 6fm\Gamma 6fm\Gamma 7fm\Gamma 4 = fm\Gamma 2fm\Gamma 4fm\Gamma 5jfm\Gamma 5fm\Gamma 6fm\Gamma 7fm\Gamma 8fm\Gamma 7fm\Gamma 4 = fm\Gamma 2fm\Gamma 4fm\Gamma 5jfm\Gamma 5fm\Gamma 5fm\Gamma 8fm\Gamma 7fm\Gamma 4: Note that the last two bits of fm\Gamma 4 are complements of the last two bits of fm\Gamma 5. Hence it follows from the last line of (7) and from Lemma 2 that precisely Fm\Gamma 5 squares of length 2Fm\Gamma 5 straddle the boundary. From the first and last lines of (7) it is easy to deduce that exactly Fm\Gamma 4 \Gamma Fm\Gamma 5 + 1 = Fm\Gamma 6 + 1 of the boundary squares of length 2Fm\Gamma 5 lie in the copy of fm\Gamma 2 which straddles the boundary of fm, so are of type 1. The rest, namely Fm\Gamma 5 \Gamma (Fm\Gamma 6 + 1) = Fm\Gamma 7 \Gamma 1, are of type 2. \Xi Lemma 10. For m * 7, fm contains Fm\Gamma 6 \Gamma 1 boundary squares of type 2 of length 2Fm\Gamma 4. Proof. From (5) and the end of the proof of Lemma 1, we have, for m * 7, (8) f m = fm\Gamma 2fm\Gamma 3jfm\Gamma 4fm\Gamma 5fm\Gamma 4 = fm\Gamma 3f3m\Gamma 4fm\Gamma 7fm\Gamma 6fm\Gamma 5fm\Gamma 4: A square of length 2Fm\Gamma 4 beginning at any position ^ Fm\Gamma 1 \Gamma 2Fm\Gamma 4 + 1, lies entirely to the left of the boundary of fm, so is not a boundary square. Next consider the interval I = [Fm\Gamma 1 \Gamma 2Fm\Gamma 4 + 2; Fm\Gamma 2]. Note that every square of length 2Fm\Gamma 4 in this interval is a boundary square. None of them is of type 1, since the last begins at the last bit of Fm\Gamma 2, which, by the first line of (8), is just before the beginning of the copy of fm\Gamma 2 which straddles the boundary. Now Fm\Gamma 1 \Gamma 2Fm\Gamma 4 + 2 = 2Fm\Gamma 3 \Gamma Fm\Gamma 4 + 2 ? Fm\Gamma 3, and the length of I is Fm\Gamma 2 \Gamma Fm\Gamma 1 + 2Fm\Gamma 4 \Gamma 1 = Fm\Gamma 6 \Gamma 1. We thus see from the second line of (8), using Lemma 2, that each bit in this interval is the beginning of a square of length 2fm\Gamma 4. All of these are type 2 squares. 9 A square of length 2Fm\Gamma 4 beginning at any position in the interval [Fm\Gamma 2 + 1; 2Fm\Gamma 2 \Gamma 2Fm\Gamma 4] = [Fm\Gamma 2 + 1; 2Fm\Gamma 3] lies entirely within the copy of fm\Gamma 2 which straddles the boundary, and so is of type 1. Now consider words of length 2Fm\Gamma 4 beginning in the interval [2Fm\Gamma 3 + 1; Fm\Gamma 1] = [Fm\Gamma 1 \Gamma Fm\Gamma 4 + 1; Fm\Gamma 1]. The first half of every such word w has a bit at position Fm\Gamma 1, and its second half has a bit at position Fm\Gamma 1 + Fm\Gamma 4 = 2Fm\Gamma 2. These are the terminal bits of the words fm\Gamma 1 and fm\Gamma 2 respectively by (2) of Lemma 4. For w to be a square, these two bits have to be the same. Hoewever, it is easily verified, by induction, that fi ends in 0 if i is even, and in 1 if i is odd, so they cannot be the same. Thus there are no squares in this interval. A square of length 2Fm\Gamma 4 beginning at any position * Fm\Gamma 1 + 1 lies entirely to the right of the boundary, so is not a boundary square. This completes the left-to-right scan of fm. \Xi Lemma 11. For m * 6, fm contains Fm\Gamma 4 + 1 boundary squares of type 2 of length 2Fm\Gamma 3, and Fm\Gamma 3 \Gamma 1 boundary squares of type 2 of length 2Fm\Gamma 2. Proof. Lemma 6 gives a complete panorama of the locations of all the conjugates of fm\Gamma 3. By the "folklore" result, cited at the end of the proof of Theorem 1, this picture exhausts all squares of length 2Fm\Gamma 3. The first batch of squares of length 2Fm\Gamma 3 ends at (Fm\Gamma 4 \Gamma 1) + 2Fm\Gamma 3 \Gamma 1 = Fm\Gamma 1 \Gamma 2, so these are not boundary squares. The next batch begins at Fm\Gamma 2+1. Since Fm\Gamma 2+1+2Fm\Gamma 3 \Gamma 1 = Fm\Gamma 1+ Fm\Gamma 3, the first of them is a boundary square. Since the last begins at Fm\Gamma 2 + Fm\Gamma 4 + 1 ! Fm\Gamma 1, all these Fm\Gamma 4 + 1 squares are, in fact, boundary squares of type 2: they cannot be of type 1, since they are too long to fit in the central copy of fm\Gamma 2. Since 2Fm\Gamma 2 ? Fm\Gamma 1 and Fm\Gamma 3 \Gamma 1 ! Fm\Gamma 1, we see from Lemma 7 that all of the Fm\Gamma 3 \Gamma 1 squares of length 2Fm\Gamma 2 are boundary squares, and clearly they are of type 2. \Xi Collecting the results of Lemmas 9-11, we get, B2 = (Fm\Gamma 7 \Gamma 1) + (Fm\Gamma 6 \Gamma 1) + (Fm\Gamma 4 + 1) + (Fm\Gamma 3 \Gamma 1) = Fm\Gamma 5 + Fm\Gamma 2 \Gamma 2 = 2(Fm\Gamma 3 \Gamma 1): From (4) and (6) we thus get the recurrence, (9) R(n) \Gamma R(n \Gamma 1) \Gamma 2R(n \Gamma 2) + R(n \Gamma 3) + R(n \Gamma 4) = 2(Fn\Gamma 3 \Gamma 1): Note that (9) agrees with Table 4 for n * 7 (but not for n = 6). Our last lemma is of a technical nature. Lemma 12. For n * 2, 'n = Fn\Gamma 1' + Fn\Gamma 2, ' n = Fn\Gamma 1' + Fn\Gamma 2. Proof. Induction on n. \Xi Proof of Theorem 2. Replacing n by n \Gamma 1 in (9), we get: R(n \Gamma 1) \Gamma R(n \Gamma 2) \Gamma 2R(n \Gamma 3) + R(n \Gamma 4) + R(n \Gamma 5) = 2(Fn\Gamma 4 \Gamma 1): Subtracting from (9) we then get: (10) R(n) \Gamma 2R(n \Gamma 1) \Gamma R(n \Gamma 2) + 3R(n \Gamma 3) \Gamma R(n \Gamma 5) = 2Fn\Gamma 5: This recurrence agrees with Table 3 for n * 8, but not for n = 7. The characteristic polynomial of (10) is: x5 \Gamma 2x4 \Gamma x3 + 3x2 \Gamma 1, and its roots are -1, 1 (double root), ' and '. Thus for the 10 homogeneous part we have a solution of the form c1(\Gamma 1)n + (c2 + c3n) + c4'n + c5' n. (See e.g., [Liu1968] for methods to solve linear recurrences.) We assume that a particular solution has the form a0nFn + a1(n \Gamma 1)Fn\Gamma 1. Substituting into (10) and collecting terms, we then get: n\Gamma a0(Fn \Gamma 2Fn\Gamma 1 \Gamma Fn\Gamma 2 + 3Fn\Gamma 3 \Gamma Fn\Gamma 5) + a1(Fn\Gamma 1 \Gamma 2Fn\Gamma 2 \Gamma Fn\Gamma 3 + 3Fn\Gamma 4 \Gamma Fn\Gamma 6)\Delta + a0(2Fn\Gamma 1 + 2Fn\Gamma 2 \Gamma 9Fn\Gamma 3 + 5Fn\Gamma 5) + a1(\Gamma Fn\Gamma 1 + 4Fn\Gamma 2 + 3Fn\Gamma 3 \Gamma 12Fn\Gamma 4 + 6Fn\Gamma 6) = 2Fn\Gamma 5: The coefficient of n ought to vanish, and it is easy to verify that Fn\Gamma 2Fn\Gamma 1\Gamma Fn\Gamma 2+3Fn\Gamma 3\Gamma Fn\Gamma 5 is indeed identically 0. Simplifying we get, (Fn\Gamma 3 + Fn\Gamma 5)a0 + (2Fn\Gamma 3 \Gamma 3Fn\Gamma 5)a1 = 2Fn\Gamma 5. Putting a0 \Gamma 3a1 = 2, a0 + 2a1 = 0, gives (a0; a1) = (4=5; \Gamma 2=5), so a particular solution has the form 45 nFn \Gamma 25 (n \Gamma 1)Fn\Gamma 1. Thus the solution of (10) (n * 8) has the form R(n) = c1(\Gamma 1)n + c2 + c3n + c4'n + c5' n + 45 nFn \Gamma 25(n \Gamma 1)Fn\Gamma 1: We now evaluate this solution for n 2 f8; : : :; 12g, using Lemma 12 to simplify the powers of ' and '. This leads to the matrix equation M C = B, where M = 266 6666 66666 4 1 1 8 472 + 212 p5 472 \Gamma 212 p5 \Gamma 1 1 9 38 + 17 p5 38 \Gamma 17 p5 1 1 10 1232 + 552 p5 1232 \Gamma 552 p5 \Gamma 1 1 11 1992 + 892 p5 1992 \Gamma 892 p5 1 1 12 161 + 72 p5 161 \Gamma 72 p5 377 7777 77777 5 ; and B = R(n) \Gamma \Gamma 45 nFn \Gamma 25 (n \Gamma 1)Fn\Gamma 1\Delta fifi12n=8 = ^ \Gamma 5095 ; \Gamma 8465 ; \Gamma 279 ; \Gamma 22865 ; \Gamma 37315 * ; where C is the vector of the coefficients ci. The Maple package then produced the solution C = ^ 0 ; 1 ; 1 ; \Gamma 2 \Gamma 425 p5 ; \Gamma 2 + 425 p5 * ; which gives, R(n) = 45 nFn \Gamma 25 (n \Gamma 1)Fn\Gamma 1 \Gamma i2 + 45p5j(Fn\Gamma 1' + Fn\Gamma 2) + i\Gamma 2 + 45p5 j(Fn\Gamma 1' + Fn\Gamma 2) + n + 1: After simplification this leads to the assertion in Theorem 2. Though it provably solves (10) for n * 8, it actually gives the correct values of R(n) for n * 3 (but not for n = 2), as can be verified using Table 3. \Xi 11 Proof of Corollary 2. We combine the two leading terms of the solution given in Theorem 2 in the form, R(n) = 25nFn + 25 nFn\Gamma 2 \Gamma 125 Fn\Gamma 1 \Gamma Fn\Gamma 2) + n + 1: This gives, R(n) = 25 (3 \Gamma ')nFn + O(Fn) = 2 5 (3 \Gamma ')Fn log' Fn + O(Fn) = 2(3 \Gamma ') 5 log2 ' Fn log2 Fn + O(Fn): \Xi References 1. [Cro1981] M. Crochemore, An optimal algorithm for computing the repetitions in a word, Inform. Process. Letters 12 (1981) 244-250. 2. [CrR1994] M. Crochemore and W. Rytter, Text Algorithms, Oxford University Press, New York, 1994. 3. [CrR1995] M. Crochemore and W. Rytter, Squares, cubes, and time-space efficient string searching, Algorithmica 13 (1995) 405-425. 4. [dLV1990] A. de Luca and S. Varricchio, A combinatorial theorem on p-power-free words and an application to semigroups, RAIRO Inform. Thor. Appl. 24 (1990) 205-228. 5. [FMT1978] A.S. Fraenkel, M. Mushkin and U. Tassa, Determination of [n`] by its sequence of differences, Canad. Math. Bull. 21 (1978) 441-446. 6. [FrS*1997] A.S. Fraenkel and R.J. Simpson, How many squares can a string contain? Preprint. 7. [IMS*1997] C.S. Iliopoulos, D. Moore and W.F. Smyth, A Characterization of the squares in a Fibonacci string, Theoret. Comput. Sci., to appear. 8. [Liu1968] C. L. Liu, Introduction to Combinatorial Mathematics, McGraw-Hill, New York, 1968. 9. [MiP1992] F. Mignosi and G. Pirillo, Repetitions in the Fibonacci infinite word, RAIRO Inform. Thor. Appl. 26 (1992) 199-204. 10. [S'e'e1985] P. S'e'ebold, Propri'et'es combinatoires des mots infinis engendr'es par certains morphismes, Report No. 85-16, LITP, Paris, 1985. 11. [Sto1976] K.B. Stolarsky, Beatty sequences, continued fractions, and certain shift operators, Canad. Math. Bull. 19 (1976) 473-482.