Skip to content

Commit bb5c243

Browse files
authored
Add Josephus Problem solver (TheAlgorithms#356)
1 parent 8d0729b commit bb5c243

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
6+
namespace Algorithms.Tests.Numeric;
7+
8+
public class JosephusProblemTest
9+
{
10+
11+
[TestCase(10, 0)]
12+
[TestCase(10, -1)]
13+
public void JosephusProblemInvalidStepSize(long groupSize, long step)
14+
{
15+
Assert.Throws(Is.TypeOf<ArgumentException>()
16+
.And.Message.EqualTo("The step cannot be smaller than 1"),
17+
delegate { JosephusProblem.FindWinner(groupSize, step); });
18+
}
19+
20+
[TestCase(10, 12)]
21+
public void JosephusProblemStepSizeGreaterThanGroup(long groupSize, long step)
22+
{
23+
Assert.Throws(Is.TypeOf<ArgumentException>()
24+
.And.Message.EqualTo("The step cannot be greater than the size of the group"),
25+
delegate { JosephusProblem.FindWinner(groupSize, step); });
26+
}
27+
28+
[TestCase(10, 2, 5)]
29+
[TestCase(10, 8, 1)]
30+
[TestCase(254, 18, 92)]
31+
[TestCase(3948, 614, 2160)]
32+
[TestCase(86521, 65903, 29473)]
33+
public void JosephusProblemWinnerCalculation(long groupSize, long step, long position)
34+
{
35+
Assert.That(JosephusProblem.FindWinner(groupSize, step), Is.EqualTo(position));
36+
}
37+
}

Algorithms/Numeric/JosephusProblem.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
namespace Algorithms.Numeric;
4+
5+
public static class JosephusProblem
6+
{
7+
/// <summary>
8+
/// Calculates the winner in the Josephus problem.
9+
/// </summary>
10+
/// <param name="n">The number of people in the initial circle.</param>
11+
/// <param name="k">The count of each step. k-1 people are skipped and the k-th is executed.</param>
12+
/// <returns>The 1-indexed position where the player must choose in order to win the game.</returns>
13+
public static long FindWinner(long n, long k)
14+
{
15+
if (k <= 0)
16+
{
17+
throw new ArgumentException("The step cannot be smaller than 1");
18+
}
19+
20+
if (k > n)
21+
{
22+
throw new ArgumentException("The step cannot be greater than the size of the group");
23+
}
24+
25+
long winner = 0;
26+
for (long stepIndex = 1; stepIndex <= n; ++stepIndex)
27+
{
28+
winner = (winner + k) % stepIndex;
29+
}
30+
31+
return winner + 1;
32+
}
33+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ find more than one implementation for the same objective but using different alg
8282
* [Miller-Rabin primality check](./Algorithms/Numeric/MillerRabinPrimalityChecker.cs)
8383
* [KrishnamurthyNumberChecker](./Algorithms/Numeric/KrishnamurthyNumberChecker.cs)
8484
* [Automorphic Number](./Algorithms/Numeric/AutomorphicNumber.cs)
85+
* [Josephus Problem](./Algorithms/Numeric/JosephusProblem.cs)
8586
* [Newton's Square Root Calculation](./Algorithms/NewtonSquareRoot.cs)
8687
* [Searches](./Algorithms/Search)
8788
* [A-Star](./Algorithms/Search/AStar/)

0 commit comments

Comments
 (0)