6 kyu

Simple Fun #140: Circle Slash

248 of 529myjinxin2015

Description:

Task

Suppose there are n people standing in a circle and they are numbered 1 through n in order.

Person 1 starts off with a sword and kills person 2. He then passes the sword to the next person still standing, in this case person 3. Person 3 then uses the sword to kill person 4, and passes it to person 5. This pattern continues around and around the circle until just one person remains.

What is the number of this person?

Example:

For n = 5, the result should be 3.

1 kills 2, passes to 3.
3 kills 4, passes to 5.
5 kills 1, passes to 3.
3 kills 5 and wins.

Input/Output

  • [input] integer n

The number of people. 1 through n standing in a circle.

1 <= n <= 1e9

  • [output] an integer

The index of the last person standing.

Puzzles

Stats:

CreatedFeb 17, 2017
PublishedFeb 17, 2017
Warriors Trained1877
Total Skips66
Total Code Submissions4177
Total Times Completed529
JavaScript Completions183
C# Completions72
Python Completions248
Ruby Completions37
PHP Completions47
Scala Completions7
Total Stars47
% of votes with a positive feedback rating89% of 118
Total "Very Satisfied" Votes95
Total "Somewhat Satisfied" Votes19
Total "Not Satisfied" Votes4
Total Rank Assessments6
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • hobovsky Avatar
  • dfhwze Avatar
  • Just4FunCoder Avatar
  • KayleighWasTaken Avatar
  • saudiGuy Avatar
  • PetitLu117 Avatar
Ad