This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.
%I A000375 #89 Jul 06 2025 22:28:36 %S A000375 0,1,2,4,7,10,16,22,30,38,51,65,80,101,113,139,159,191,221 %N A000375 Topswops (1): start by shuffling n cards labeled 1..n. If top card is m, reverse order of top m cards, then repeat. a(n) is the maximal number of steps before top card is 1. %C A000375 Knuth's algorithm can be extended by considering unsorted large unmovable segments: xxx645, e.g. will never move 6, 4, or 5. - Quan T. Nguyen, William Fahle (waf013000(AT)utdallas.edu), Oct 12 2010 %C A000375 For n=17, there are two longest-winded permutations (or orders of cards) that take 159 steps of "topswopping moves" before the top card is 1. (8 15 17 13 9 4 6 3 2 12 16 14 11 5 10 1 7) terminates at (1 6 2 4 9 3 7 8 5 10 11 12 13 14 15 16 17), and (2 10 15 11 7 14 5 16 6 4 17 13 1 3 8 9 12) terminates in sorted order, i.e., (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17). - Quan T. Nguyen, William Fahle (tuongquan.nguyen(AT)utdallas.edu), Oct 21 2010 %C A000375 Lower bounds for the next terms are a(18)>=191, a(19)>=221, a(20)>=249, a(21)>=282, a(22)>=335, a(23)>=382. - _Hugo Pfoertner_, May 21 2011; updated Oct 08 2016 %D A000375 Martin Gardner, Time Travel and Other Mathematical Bewilderments (Freeman, 1988), Chapter 6 "Combinatorial Card Problems" [based on a column that originally appeared in Scientific American, November 1974]. %D A000375 D. E. Knuth, TAOCP, Section 7.2.1.2, Problems 107-109. %H A000375 Kenneth Anderson and Duane Rettig, <a href="http://citeseerx.ist.psu.edu/pdf/70c62d4286f610203c3c4ecb31afd86c2407933a">Performing Lisp Analysis of the FANNKUCH Benchmark</a> %H A000375 David Berman, M. S. Klamkin and D. E. Knuth, <a href="http://www.jstor.org/stable/2030263">Problem 76-17. A reverse card shuffle</a>, SIAM Review 19 (1977), 739-741. Also published in: M. Klamkin, ed., Problems in Applied Mathematics: Selections from SIAM Review, SIAM, 1990; see p. 115-117. %H A000375 Zhang Desheng, <a href="https://www.diva-portal.org/smash/record.jsf?pid=diva2%3A1563902">The Topswop Forest</a>, bachelor thesis, Linnaeus Univ. (Sweden, 2021). Mentions the terms of this sequence. %H A000375 Brent Fulgham, <a href="https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/fannkuchredux.html">fannkuch-redux benchmark</a>, The Computer Language Benchmarks Game %H A000375 Kento Kimura, Atsuki Takahashi, Tetsuya Araki, and Kazuyuki Amano, <a href="https://arxiv.org/abs/2103.08346">Maximum Number of Steps of Topswops on 18 and 19 Cards</a>, arXiv:2103.08346 [cs.DM], 2021. %H A000375 Kento Kimura, <a href="https://gitlab.com/kkimura/tswops">kimurakento / tswops</a>, 2021. %H A000375 D. E. Knuth, <a href="http://www-cs-faculty.stanford.edu/~knuth/programs.html">Downloadable programs</a> %H A000375 Yuichi Komano and Takaaki Mizuki, <a href="https://doi.org/10.1007/978-3-031-21280-2_30">Physical Zero-Knowledge Proof Protocol for Topswops</a>, Int'l Conf. Info. Sec. Practice and Experience (ISPEC 2022) Lecture Notes in Comp. Sci. book series (LNCS Vol. 13620) Springer, Cham, 537-553. %H A000375 L. Morales and H. Sudborough, <a href="https://doi.org/10.1016/j.tcs.2010.08.011">A quadratic lower bound for Topswops</a>, Theor. Comp. Sci 411 (2010) 3965-3970. %H A000375 Andy Pepperdine, <a href="http://www.jstor.org/stable/3619674">Topswops</a>, Mathematical Gazette 73 (1989), 131-133. %e A000375 From _R. K. Guy_, Jan 24 2007: (Start) %e A000375 With 4 cards there are just two permutations which require 4 flips: %e A000375 3142 --> 4132 --> 2314 --> 3214 --> 1234 %e A000375 2413 --> 4213 --> 3124 --> 2134 --> 1234 %e A000375 In these cases the deck finishes up sorted. But this is not always the case - see A000376. (End) %t A000375 Table[Max@ Map[Length@ NestWhileList[Flatten@{Reverse@Take[#, First@ #], Take[#, -(Length@ # - First@ #)]} &, #, First@ # != 1 &] - 1 &, Permutations@ Range@ n], {n, 8}] (* _Michael De Vlieger_, Oct 08 2016 *) %o A000375 (PARI) a(n)=my(s,t,v);for(i=1,n!,v=numtoperm(n,i);t=0;while(v[1]>1,v=if(v[1]<n,concat(Vecrev(v[1..v[1]]),v[v[1]+1..n]),Vecrev(v));t++);s=max(s,t));s \\ _Charles R Greathouse IV_, Oct 14 2013 %o A000375 (Python) %o A000375 from itertools import permutations as P %o A000375 def ts(d, var=1): # algorithm VARiation %o A000375 s, m = 0, d[0] %o A000375 while m != 1: %o A000375 d = (d[:m])[::-1] + d[m:] %o A000375 s, m = s+1, d[0] %o A000375 if var==2: return s*(list(d)==sorted(d)) %o A000375 return s %o A000375 def a(n): %o A000375 return max(ts(d) for d in P(range(1, n+1))) %o A000375 print([a(n) for n in range(1, 11)]) # _Michael S. Branicky_, Dec 11 2020 %Y A000375 Cf. A000376 (a variation), A123398 (number of solutions). %K A000375 nonn,hard,nice,more %O A000375 1,3 %A A000375 _Bill Blewett_ and Mike Toepke [mtoepke(AT)microsoft.com] %E A000375 One more term from _James Kilfiger_, Jul 1997 %E A000375 113 from _William Rex Marshall_, Mar 27 2001 %E A000375 139 from _Don Knuth_, Aug 25 2001 %E A000375 Added one new term by improved branch and bound using various new insights. - Quan T. Nguyen, William Fahle (waf013000(AT)utdallas.edu), Oct 12 2010 %E A000375 a(18)-a(19) from Kimura et al. added by _Andrey Zabolotskiy_, Mar 24 2021