GithubHelp home page GithubHelp logo

kyriakos-2004 / josephus_problem Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 3 KB

This program calculates the surviving position in the Josephus problem with two ways.

License: MIT License

C++ 100.00%
cpp math josephus-problem

josephus_problem's Introduction

Calculate the surviving position in the Josephus problem

This program calculates the surviving position in the Josephus problem with two ways. The first one by using observational mathematics and patterns, while the second one using bit shifting.

What is the Josephus problem?

A bit of history. Flavius Josephus was a Jewish historian living in the 1st century. When the Yodfat was under siege by the Romans, he and 40 soldiers were trapped in a cave. Instead of surrendering to the Romans, they chose suicide instead. The way to do this was that they all stand in a circle and starting on the top, each person will kill the one next to them. The remaining alive soldiers will form a smaller circle and the one who was next, will kill the one standing next to them. This will go on and on until only one man remains. This was a trick, as Josephus did not want to commit suicide and he would rather surrender himself. He thought that either by luck or the hand of God, he might be spared in the circle and surrender.

In case the number of soldiers in the circle is a power of two, then the Josephus position (or the last man standing position) is always the starting position. The most interesting part of this problem is that if you write the number of soldiers in binary and then take the most significant bit (the one in the far left) and bring it to the end to become the least significant bit, then the number that occurs in binary is the Josephus position. This is the example:

Number of soldiers: 41

41 in binary: 101001

Move the most significant bit to the end: 010011

The answer is : 19

The reason the bit shifting is true is due to the fact that the whole problem can be written as an equation that depends only on the number of soldiers. 2m is described as the largest power of two that is less or equal to the number of soldiers. This is an algorithmic description, however. Taken from mathematics, m in this case is the integer part of the logarithm with base 2 of n. Programmatically, this can be written as floor(log2 n). Through this, we can see that if we combine the two previous equations:

n = 2m + k

w = 2k + 1

we will get:

w = 2(n โ€“ 2m) + 1

and if we apply what we have just found out:

w = 2(n โ€“ 2floor(log2(n))) + 1

Inspiration and further reading

The following video from the channel Numberphile and Professor Elman, was the inspiration behind this code. Numberphile Video - The Josephus Problem

The below page explains thoroughly why the shifting the most significant bit to least significant bit always works. The Josephus Problem - Binary analysis

License

This project is licensed under the MIT license.

josephus_problem's People

Contributors

kyriakos-2004 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.