Skip to content

Instantly share code, notes, and snippets.

@vireshas
Created January 25, 2017 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vireshas/ce07d5ff9d6fa577aaa168d1a68df877 to your computer and use it in GitHub Desktop.
Save vireshas/ce07d5ff9d6fa577aaa168d1a68df877 to your computer and use it in GitHub Desktop.
Strange Counter
Bob has a strange counter. At the first second, , it displays the number . At each subsequent second, the number displayed by the counter decrements by .
The counter counts down in cycles. In the second after the counter counts down to , the number becomes the initial number for that countdown cycle and then continues counting down from the new initial number in a new cycle. The diagram below shows the counter values for each time in the first three cycles:
https://s3.amazonaws.com/hr-challenge-images/22185/1469447349-bae87a5071-strange1.png
Given a time, , find and print the value displayed by the counter at time .
Input Format
A single integer denoting the value of .
Constraints
Subtask
for of the maximum score.
Output Format
Print the value displayed by the strange counter at the given time .
Sample Input
4
Sample Output
6
#!/bin/ruby
t = gets.strip.to_i - 1
s = 3
while s <= t
t -= s
s *= 2
end
puts s - t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment