A New 0(klog n) Algorithm for Josephus Problem

Hikaru Manabe,Ryohei Miyadera,Yuji Sasaki,Shoei Takahashi,Yuki Tokuni
2024-11-10
Abstract:We present a new O(k log n) algorithm of the Josephus problem. The time complexity of our algorithm is O(k log n), and this time complexity is on a par with the existing O(k log n) algorithm. We do not have any recursion overhead or stack overflow because we do not use any recursion. Therefore, the space complexity of our algorithm is O(1), and ours is better than the existing O(k log n) algorithm in this respect. When k is small and n is large, our algorithm is better than the existing O(k log n) algorithm. This new algorithm is based on a relation between the Josephus problem and a maximum Nim of combinatorial game theory.
Data Structures and Algorithms
What problem does this paper attempt to address?
This paper attempts to solve the problem of efficient algorithms for determining the last remaining number in the Josephus Problem. Specifically, the paper proposes a new algorithm with a time complexity of \(O(k \log n)\) to solve the problem of removing the \(k\)-th number each time in a circle composed of \(n\) numbers. ### Background of the Josephus Problem The Josephus Problem is described as follows: Given \(n\) numbers numbered from 1 to \(n\) arranged in a circle, starting from the number numbered 1, remove the \(k\)-th number each time until only one number remains. The goal of the problem is to determine the last remaining number. ### Main Contributions of the Paper 1. **Propose a New Algorithm**: - The paper proposes a new algorithm based on the Maximum Nim game. - The time complexity of this algorithm is \(O(k \log n)\), which is comparable to existing algorithms. - The space complexity of this algorithm is \(O(1)\), which is better than the existing \(O(k \log n)\) algorithms because it does not use recursion, avoiding the problems of recursive overhead and stack overflow. 2. **Specific Steps of the Algorithm**: - Initialize \(x = 0\). - Update \(x\) using the formula \(h_k(x)=x + \left\lfloor\frac{x}{k - 1}\right\rfloor+ 1\) until \(nk - n\leq x\) is satisfied. - The last remaining number is \(nk - x\). 3. **Performance Comparison**: - Compared with the existing \(O(n)\) and \(O(k \log n)\) algorithms, the new algorithm performs better when dealing with large - scale data. - When \(n\) is large and \(k\) is small, the performance of the new algorithm is particularly outstanding. ### Formula Summary - \(h_k(x)=x + \left\lfloor\frac{x}{k - 1}\right\rfloor+ 1\) - When \(h_{p - 1}^k(0)<n(k - 1)\leq h_p^k(0)\), the last remaining number is \(nk - h_p^k(0)\). Through these improvements, the paper provides a more efficient solution with lower space complexity, especially suitable for solving the Josephus Problem with large - scale data.