cp's OEIS Frontend

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.

Showing 1-6 of 6 results.

A006257 Josephus problem: a(2*n) = 2*a(n)-1, a(2*n+1) = 2*a(n)+1.

Original entry on oeis.org

0, 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29
Offset: 0

Views

Author

Keywords

Comments

Write the numbers 1 through n in a circle, start at 1 and cross off every other number until only one number is left.
A version of the children's game "One potato, two potato, ...".
a(n)/A062383(n) = (0, 0.1, 0.01, 0.11, 0.001, ...) enumerates all binary fractions in the unit interval [0, 1). - Fredrik Johansson, Aug 14 2006
Iterating a(n), a(a(n)), ... eventually leads to 2^A000120(n) - 1. - Franklin T. Adams-Watters, Apr 09 2010
By inspection, the solution to the Josephus Problem is a sequence of odd numbers (from 1) starting at each power of 2. This yields a direct closed form expression (see formula below). - Gregory Pat Scandalis, Oct 15 2013
Also zero together with a triangle read by rows in which row n lists the first 2^(n-1) odd numbers (see A005408), n >= 1. Row lengths give A011782. Right border gives A000225. Row sums give A000302, n >= 1. See example. - Omar E. Pol, Oct 16 2013
For n > 0: a(n) = n + 1 - A080079(n). - Reinhard Zumkeller, Apr 14 2014
In binary, a(n) = ROL(n), where ROL = rotate left = remove the leftmost digit and append it to the right. For example, n = 41 = 101001_2 => a(n) = (0)10011_2 = 19. This also explains FTAW's comment above. - M. F. Hasler, Nov 02 2016
In the under-down Australian card deck separation: top card on bottom of a deck of n cards, next card separated on the table, etc., until one card is left. The position a(n), for n >= 1, from top will be the left over card. See, e.g., the Behrends reference, pp. 156-164. For the down-under case see 2*A053645(n), for n >= 3, n not a power of 2. If n >= 2 is a power of 2 the botton card survives. - Wolfdieter Lang, Jul 28 2020

Examples

			From _Omar E. Pol_, Jun 09 2009: (Start)
Written as an irregular triangle the sequence begins:
  0;
  1;
  1,3;
  1,3,5,7;
  1,3,5,7,9,11,13,15;
  1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31;
  1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,
   43,45,47,49,51,53,55,57,59,61,63;
...
(End)
From _Omar E. Pol_, Nov 03 2018: (Start)
An illustration of initial terms, where a(n) is the area (or number of cells) in the n-th region of the structure:
   n   a(n)       Diagram
   0    0     _
   1    1    |_|_ _
   2    1      |_| |
   3    3      |_ _|_ _ _ _
   4    1          |_| | | |
   5    3          |_ _| | |
   6    5          |_ _ _| |
   7    7          |_ _ _ _|
(End)
		

References

  • Erhard Behrends, Der mathematische Zauberstab, Rowolth Taschenbuch Verlag, rororo 62902, 4. Auflage, 2019, pp. 156-164. [English version: The Math Behind the Magic, AMS, 2019.]
  • R. L. Graham, D. E. Knuth and O. Patashnik, Concrete Mathematics. Addison-Wesley, Reading, MA, 1990, p. 10.
  • M. S. Petković, "Josephus problem", Famous Puzzles of Great Mathematicians, page 179, Amer. Math. Soc. (AMS), 2009.
  • Michel Rigo, Formal Languages, Automata and Numeration Systems, 2 vols., Wiley, 2014. Mentions this sequence - see "List of Sequences" in Vol. 2.
  • N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
  • Paul Weisenhorn, Josephus und seine Folgen, MNU, 59(2006), pp. 18-19.

Crossrefs

Second column, and main diagonal, of triangle A032434.
Cf. A181281 (with s=5), A054995 (with s=3).
Column k=2 of A360099.

Programs

  • Coq
    Require Import ZArith.
    Fixpoint a (n : positive) : Z :=
    match n with
    | xH => 1
    | xI n' => (2*(a n') + 1)%Z
    | xO n' => (2*(a n') - 1)%Z
    end.
    (* Stefan Haan, Aug 27 2023 *)
  • Haskell
    a006257 n = a006257_list !! n
    a006257_list =
       0 : 1 : (map (+ 1) $ zipWith mod (map (+ 1) $ tail a006257_list) [2..])
    -- Reinhard Zumkeller, Oct 06 2011
    
  • Magma
    [0] cat [2*(n-2^Floor(Log(2,n)))+1: n in [1..100]]; // Vincenzo Librandi, Jan 14 2016
    
  • Maple
    a(0):=0: for n from 1 to 100 do a(n):=(a(n-1)+1) mod n +1: end do:
    seq(a(i),i=0..100); # Paul Weisenhorn, Oct 10 2010; corrected by Robert Israel, Jan 13 2016
    A006257 := proc(n)
        convert(n,base,2) ;
        ListTools[Rotate](%,-1) ;
        add( op(i,%)*2^(i-1),i=1..nops(%)) ;
    end proc: # R. J. Mathar, May 20 2016
    A006257 := n -> 2*n  - Bits:-Iff(n, n):
    seq(A006257(n), n=0..78); # Peter Luschny, Sep 24 2019
  • Mathematica
    Table[ FromDigits[ RotateLeft[ IntegerDigits[n, 2]], 2], {n, 0, 80}] (* Robert G. Wilson v, Sep 21 2003 *)
    Flatten@Table[Range[1, 2^n - 1, 2], {n, 0, 5}] (* Birkas Gyorgy, Feb 07 2011 *)
    m = 5; Range[2^m - 1] + 1 - Flatten@Table[Reverse@Range[2^n], {n, 0, m - 1}] (* Birkas Gyorgy, Feb 07 2011 *)
  • PARI
    a(n)=sum(k=1,n,if(bitxor(n,k)Paul D. Hanna
    
  • PARI
    a(n)=if(n, 2*n-2^logint(2*n,2)+1, 0) \\ Charles R Greathouse IV, Oct 29 2016
    
  • Python
    import math
    def A006257(n):
         return 0 if n==0 else 2*(n-2**int(math.log(n,2)))+1 # Indranil Ghosh, Jan 11 2017
    
  • Python
    def A006257(n): return bool(n&(m:=1<Chai Wah Wu, Jan 22 2023
    (C#)
    static long cs_A006257(this long n) => n == 0 ? 0 : 1 + (1 + (n - 1).cs_A006257()) % n; // Frank Hollstein, Feb 24 2021
    

Formula

To get a(n), write n in binary, rotate left 1 place.
a(n) = 2*A053645(n) + 1 = 2(n-msb(n))+1. - Marc LeBrun, Jul 11 2001. [Here "msb" = "most significant bit", A053644.]
G.f.: 1 + 2/(1-x) * ((3*x-1)/(2-2*x) - Sum_{k>=1} 2^(k-1)*x^2^k). - Ralf Stephan, Apr 18 2003
a(n) = number of positive integers k < n such that n XOR k < n. a(n) = n - A035327(n). - Paul D. Hanna, Jan 21 2006
a(n) = n for n = 2^k - 1. - Zak Seidov, Dec 14 2006
a(n) = n - A035327(n). - K. Spage, Oct 22 2009
a(2^m+k) = 1+2*k; with 0 <= m and 0 <= k < 2^m; n = 2^m+k; m = floor(log_2(n)); k = n-2^m; a(n) = ((a(n-1)+1) mod n) + 1; a(1) = 1. E.g., n=27; m=4; k=11; a(27) = 1 + 2*11 = 23. - Paul Weisenhorn, Oct 10 2010
a(n) = 2*(n - 2^floor(log_2(n))) + 1 (see comment above). - Gregory Pat Scandalis, Oct 15 2013
a(n) = 0 if n = 0 and a(n) = 2*a(floor(n/2)) - (-1)^(n mod 2) if n > 0. - Marek A. Suchenek, Mar 31 2016
G.f. A(x) satisfies: A(x) = 2*A(x^2)*(1 + x) + x/(1 + x). - Ilya Gutkovskiy, Aug 31 2019
For n > 0: a(n) = 2 * A062050(n) - 1. - Frank Hollstein, Oct 25 2021

Extensions

More terms from Robert G. Wilson v, Sep 21 2003

A321298 Triangle read by rows: T(n,k) is the number of the k-th eliminated person in the Josephus elimination process for n people and a count of 2, 1 <= k <= n.

Original entry on oeis.org

1, 2, 1, 2, 1, 3, 2, 4, 3, 1, 2, 4, 1, 5, 3, 2, 4, 6, 3, 1, 5, 2, 4, 6, 1, 5, 3, 7, 2, 4, 6, 8, 3, 7, 5, 1, 2, 4, 6, 8, 1, 5, 9, 7, 3, 2, 4, 6, 8, 10, 3, 7, 1, 9, 5, 2, 4, 6, 8, 10, 1, 5, 9, 3, 11, 7, 2, 4, 6, 8, 10, 12, 3, 7, 11, 5, 1, 9, 2, 4, 6, 8, 10, 12, 1, 5, 9, 13, 7, 3, 11, 2, 4, 6, 8, 10, 12, 14
Offset: 1

Views

Author

Zeph L. Turner, Nov 02 2018

Keywords

Comments

In the Josephus elimination process for n and k, the numbers 1 through n are written in a circle. A pointer starts at position 1. Each turn, the pointer skips (k-1) non-eliminated number(s) going around the circle and eliminates the k-th number, until no numbers remain. This sequence represents the triangle J(n, i), where n is the number of people in the circle, i is the turn number, and k is fixed at 2 (every other number is eliminated).

Examples

			Triangle begins:
  1;
  2, 1;
  2, 1, 3;
  2, 4, 3, 1;
  2, 4, 1, 5,  3;
  2, 4, 6, 3,  1,  5;
  2, 4, 6, 1,  5,  3, 7;
  2, 4, 6, 8,  3,  7, 5, 1;
  2, 4, 6, 8,  1,  5, 9, 7,  3;
  2, 4, 6, 8, 10,  3, 7, 1,  9,  5;
  2, 4, 6, 8, 10,  1, 5, 9,  3, 11, 7;
  2, 4, 6, 8, 10, 12, 3, 7, 11,  5, 1, 9;
  2, 4, 6, 8, 10, 12, 1, 5,  9, 13, 7, 3, 11;
  ...
For n = 5, to get the entries in 5th row from left to right, start with (^1, 2, 3, 4, 5) and the pointer at position 1, indicated by the caret. 1 is skipped and 2 is eliminated to get (1, ^3, 4, 5). (The pointer moves ahead to the next "live" number.) On the next turn, 3 is skipped and 4 is eliminated to get (1, 3, ^5). Then 1, 5, and 3 are eliminated in that order (going through (^3, 5) and (^3)). This gives row 5 of the triangle and entries a(11) through a(15) in this sequence.
		

Crossrefs

The right border of this triangle is A006257.
Cf. A032434, A054995, A181281, A225381, A378635 (row permutation inverses).

Programs

  • Mathematica
    Table[Rest@ Nest[Append[#1, {Delete[#2, #3 + 1], #2[[#3 + 1]], #3}] & @@ {#, #[[-1, 1]], Mod[#[[-1, -1]] + 1, Length@ #[[-1, 1]]]} &, {{Range@ n, 0, 0}}, n][[All, 2]], {n, 14}] // Flatten (* Michael De Vlieger, Nov 13 2018 *)
  • Python
    def A321298(n,k):
        if 2*k<=n: return 2*k
        n2,r=divmod(n,2)
        if r==0: return 2*A321298(n2,k-n2)-1
        if k==n2+1: return 1
        return 2*A321298(n2,k-n2-1)+1 # Pontus von Brömssen, Sep 18 2022

Formula

From Pontus von Brömssen, Sep 18 2022: (Start)
The terms are uniquely determined by the following recursive formulas:
T(n,k) = 2*k if k <= n/2;
T(2*n,k) = 2*T(n,k-n)-1 if k > n;
T(2*n+1,k) = 2*T(n,k-n-1)+1 if k > n+1;
T(2*n+1,n+1) = 1.
(End)
From Pontus von Brömssen, Dec 11 2024: (Start)
The terms are also uniquely determined by the following recursive formulas:
T(1,1) = 1;
T(n,1) = 2 if n > 1;
T(n,k) = T(n-1,k-1)+2 if k > 1 and T(n-1,k-1) != n-1;
T(n,k) = 1 if k > 1 and T(n-1,k-1) = n-1.
T(n,A225381(n)) = 1.
T(n,A225381(n+1)-1) = n.
(End)

Extensions

Name clarified by Pontus von Brömssen, Sep 18 2022

A054995 A version of Josephus problem: a(n) is the surviving integer under the following elimination process. Arrange 1,2,3,...,n in a circle, increasing clockwise. Starting with i=1, delete the integer two places clockwise from i. Repeat, counting two places from the next undeleted integer, until only one integer remains.

Original entry on oeis.org

1, 2, 2, 1, 4, 1, 4, 7, 1, 4, 7, 10, 13, 2, 5, 8, 11, 14, 17, 20, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 1, 4, 7, 10, 13, 16, 19, 22, 25
Offset: 1

Views

Author

John W. Layman, May 30 2000

Keywords

Comments

If one counts only one place (rather than two) at each stage to determine the element to be deleted, the Josephus survivors (A006257) are obtained.

Examples

			a(5) = 4 because the elimination process gives (1^,2,3,4,5) -> (1,2,4^,5) -> (2^,4,5) -> (2^,4) -> (4), where ^ denotes the counting reference position.
a(13) = 13 => a(14) = (a(13) + 2) mod 14 + 1 = 2. - _Paul Weisenhorn_, Oct 10 2010
		

Crossrefs

Cf. A181281 (with s=5). - Paul Weisenhorn, Oct 10 2010

Programs

  • Mathematica
    (* First do *) Needs["Combinatorica`"] (* then *) f[n_] := Last@ InversePermutation@ Josephus[n, 3]; Array[f, 70] (* Robert G. Wilson v, Jul 31 2010 *)
    Table[Nest[Rest@RotateLeft[#, 2] &, Range[n], n - 1], {n, 72}] // Flatten (* Arkadiusz Wesolowski, Jan 14 2013 *)

Formula

a(n) = 3*n + 1 - floor(K(3)*(3/2)^(ceiling(log((2*n+1)/K(3))/log(3/2)))) where K(3) = (3/2)*K = 1.622270502884767... (K is the constant described in A061419); a(n) = 3n + 1 - A061419(k+1) where A061419(k+1) is the least integer such that A061419(k+1) > 2n.
a(1) = 1 and, for n > 1, a(n) = (a(n-1) + 3) mod n, if this value is nonzero, n otherwise.
a(n) = (a(n-1) + 2) mod n + 1. - Paul Weisenhorn, Oct 10 2010

A198789 Array T(n,k) read by antidiagonals: Last survivor positions in Josephus problem for n numbers and a count of k, n >= 1, k >= 1.

Original entry on oeis.org

1, 1, 2, 1, 1, 3, 1, 2, 3, 4, 1, 1, 2, 1, 5, 1, 2, 2, 1, 3, 6, 1, 1, 1, 2, 4, 5, 7, 1, 2, 1, 2, 1, 1, 7, 8, 1, 1, 3, 3, 2, 5, 4, 1, 9, 1, 2, 3, 2, 4, 1, 2, 7, 3, 10, 1, 1, 2, 3, 4, 4, 6, 6, 1, 5, 11, 1, 2, 2, 3, 1, 5, 3, 3, 1, 4, 7, 12, 1, 1, 1, 4, 2, 3, 5, 1, 8, 5, 7, 9, 13
Offset: 1

Views

Author

William Rex Marshall, Nov 21 2011

Keywords

Comments

Arrange 1, 2, 3, ..., n clockwise in a circle. Starting the count at 1, delete every k-th integer clockwise until only one remains, which is T(n,k).
The main diagonal (1, 1, 2, 2, 2, 4, 5, 4, ...) is A007495.
Concatenation of consecutive rows (up to the main diagonal) gives A032434.
The periods of the rows, (1, 2, 6, 12, 60, 60, 420, 840, ...), is given by A003418.

Examples

			.n\k  1  2  3  4  5  6  7  8  9 10
----------------------------------
.1 |  1  1  1  1  1  1  1  1  1  1
.2 |  2  1  2  1  2  1  2  1  2  1
.3 |  3  3  2  2  1  1  3  3  2  2
.4 |  4  1  1  2  2  3  2  3  3  4
.5 |  5  3  4  1  2  4  4  1  2  4
.6 |  6  5  1  5  1  4  5  3  5  2
.7 |  7  7  4  2  6  3  5  4  7  5
.8 |  8  1  7  6  3  1  4  4  8  7
.9 |  9  3  1  1  8  7  2  3  8  8
10 | 10  5  4  5  3  3  9  1  7  8
		

Crossrefs

Cf. A000027 (k = 1), A006257 (k = 2), A054995 (k = 3), A088333 (k = 4), A181281 (k = 5), A360268 (k = 6), A178853 (k = 7), A109630 (k = 8).
Cf. A003418, A007495 (main diagonal), A032434, A198788, A198790.

Programs

  • Mathematica
    T[n_, k_] := T[n, k] = If[n == 1, 1, Mod[T[n-1, k]+k-1, n]+1];
    Table[T[n-k+1, k], {n, 1, 13}, {k, n, 1, -1}] // Flatten (* Jean-François Alcover, Mar 04 2023 *)

Formula

T(1,k) = 1; for n > 1: T(n,k) = ((T(n-1,k) + k - 1) mod n) + 1.

A198788 Array T(k,n) read by descending antidiagonals: Last survivor positions in Josephus problem for n numbers and a count of k, n >= 1, k >= 1.

Original entry on oeis.org

1, 2, 1, 3, 1, 1, 4, 3, 2, 1, 5, 1, 2, 1, 1, 6, 3, 1, 2, 2, 1, 7, 5, 4, 2, 1, 1, 1, 8, 7, 1, 1, 2, 1, 2, 1, 9, 1, 4, 5, 2, 3, 3, 1, 1, 10, 3, 7, 2, 1, 4, 2, 3, 2, 1, 11, 5, 1, 6, 6, 4, 4, 3, 2, 1, 1, 12, 7, 4, 1, 3, 3, 5, 1, 3, 2, 2, 1, 13, 9, 7, 5, 8, 1, 5, 3
Offset: 1

Views

Author

William Rex Marshall, Nov 21 2011

Keywords

Comments

Arrange 1, 2, 3, ... n clockwise in a circle. Starting the count at 1, delete every k-th integer clockwise until only one remains, which is T(k,n).
The main diagonal of the array (1, 1, 2, 2, 2, 4, 5, 4, ...) is A007495.
Consecutive columns down to the main diagonal (1, 2, 1, 3, 3, 2, 4, 1, 1, 2, ...) is A032434.
Period lengths of columns (1, 2, 6, 12, 60, 60, 420, 840, ...) is A003418.

Examples

			.k\n  1  2  3  4  5  6  7  8  9 10
----------------------------------
.1 |  1  2  3  4  5  6  7  8  9 10  A000027
.2 |  1  1  3  1  3  5  7  1  3  5  A006257
.3 |  1  2  2  1  4  1  4  7  1  4  A054995
.4 |  1  1  2  2  1  5  2  6  1  5  A088333
.5 |  1  2  1  2  2  1  6  3  8  3  A181281
.6 |  1  1  1  3  4  4  3  1  7  3
.7 |  1  2  3  2  4  5  5  4  2  9  A178853
.8 |  1  1  3  3  1  3  4  4  3  1  A109630
.9 |  1  2  2  3  2  5  7  8  8  7
10 |  1  1  2  4  4  2  5  7  8  8
		

Crossrefs

Cf. A000027 (k = 1), A006257 (k = 2), A054995 (k = 3), A088333 (k = 4), A181281 (k = 5), A178853 (k = 7), A109630 (k = 8).
Cf. A003418, A007495 (main diagonal), A032434, A198789, A198790.

Formula

T(k,1) = 1;
for n > 1: T(k,n) = ((T(k,n-1) + k - 1) mod n) + 1.

A360268 A version of the Josephus problem: a(n) is the surviving integer under the following elimination process. Arrange 1,2,3,...,n in a circle, increasing clockwise. Starting with i=1, delete the integer 5 places clockwise from i. Repeat, counting 5 places from the next undeleted integer, until only one integer remains.

Original entry on oeis.org

1, 1, 1, 3, 4, 4, 3, 1, 7, 3, 9, 3, 9, 1, 7, 13, 2, 8, 14, 20, 5, 11, 17, 23, 4, 10, 16, 22, 28, 4, 10, 16, 22, 28, 34, 4, 10, 16, 22, 28, 34, 40, 3, 9, 15, 21, 27, 33, 39, 45, 51, 5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 1, 7, 13, 19, 25
Offset: 1

Views

Author

Benjamin Lilley, Jan 31 2023

Keywords

Examples

			a(7) = 3 because the elimination process gives (^1,2,3,4,5,6,7) -> (1,2,3,4,5,^7) -> (1,2,3,4,^7) -> (^1,2,3,4) -> (1,^3,4) -> (^3,4) -> (3), where ^ denotes the counting reference position.
a(13) = 9 => a(14) = (a(13) + 5) mod 14 + 1 = 1.
		

Crossrefs

6th column of A198789.

Programs

  • Mathematica
    a[1] = 1; a[n_] := a[n] = Mod[a[n - 1] + 5, n] + 1; Array[a, 100] (* Amiram Eldar, Feb 03 2023 *)
  • Python
    def A360268_up_to_n(n):
      val = 1
      return [val := (val + 5) % i + 1 for i in range(1,n+1)]

Formula

a(n) = (a(n-1) + 5) mod n + 1 if n > 1, a(1) = 1.
Showing 1-6 of 6 results.