%I #26 Jul 14 2020 07:02:34
%S 1,1,3,1,2,3,5,7,9,1,2,3,4,5,6,7,8,9,11,13,15,17,19,21,23,25,27,1,2,3,
%T 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,29,
%U 31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69
%N The n-cowboy shootout problem: a(3^k) = 3^k, a((3^k)+b) = b if (3^k)+b <= 2*3^k, otherwise a((3^k)+b) = 2*b-3^k, where b is a positive integer.
%C Arrange n cowboys in a circle, each takes it in turn to shoot the cowboy directly opposite them, so there is only one cowboy shooting at a time. If there is a gap directly opposite them, because the number of remaining cowboys is odd, they shoot the cowboy on the left of the gap. Turns progress clockwise from the starting cowboy. Every time a cowboy is killed the circle is rearranged so every cowboy is evenly spaced.
%C a(n) is the starting position of the surviving cowboy.
%C A variant of the Josephus problem.
%H JWSon, <a href="https://www.reddit.com/r/mathriddles/comments/gct41o/turnbased_cowboy_standoff/">Turn-Based Cowboy Standoff</a>
%H Wikipedia <a href="https://en.wikipedia.org/wiki/Josephus_problem">Josephus problem</a>
%H <a href="/index/J#Josephus">Index entries for sequences related to the Josephus Problem</a>
%e a(4) = 1: The first cowboy shoots the 3rd cowboy opposite him. The 2nd cowboy would then be aiming in a gap between the 1st and the 4th cowboy so shoots the cowboy on the left of the gap (the 4th cowboy). We are now back with the 1st cowboy who shoots the only remaining cowboy (the 2nd) so wins.
%o (Python 3)
%o def highest_power_of_3(n):
%o option = 0
%o while 3**option <= n:
%o option +=1
%o return 3 ** (option -1)
%o def answer(n):
%o x = highest_power_of_3(n)
%o if x == n:
%o return x
%o else:
%o if n < 2 * x:
%o return n%x
%o else:
%o return x + 2 * (n%x)
%o number_of_terms = 100
%o answers = []
%o for i in range(1, number_of_terms + 1):
%o answers.append(answer(i))
%o print(answers)
%Y Cf. A006257, A336233.
%K nonn
%O 1,3
%A _Neil Hacker_, May 05 2020