Consider a sequence, which is formed by the following rule: next term is taken as the smallest possible non-negative integer, which is not yet in the sequence, so that `no 3` terms of sequence form an arithmetic progression.

## Example

`f(0) = 0` -- smallest non-negative  
`f(1) = 1` -- smallest non-negative, which is not yet in the sequence  
`f(2) = 3` -- since `0, 1, 2` form an arithmetic progression  
`f(3) = 4` -- neither of `0, 1, 4`, `0, 3, 4`, `1, 3, 4` form an arithmetic progression, so we can take smallest non-negative, which is larger than `3`  
`f(4) = 9` --  `5, 6, 7, 8` are not good, since `1, 3, 5`, `0, 3, 6`, `1, 4, 7`, `0, 4, 8` are all valid arithmetic progressions.  

etc...

## The task

Write a function `f(n)`, which returns the `n-th` member of sequence.

## Limitations

There are `1000` random tests with `0 <= n <= 10^9`, so you should consider algorithmic complexity of your solution.

Difficulty: introductory

Starter code:

def sequence(n):
	

Solutions:
sequence=lambda n:int(format(n,'b'),3)

def sequence(n):
    return int(format(n, 'b'), 3)

sequence=lambda n:int(bin(n)[2:],3)

# A005836
def sequence(n):
    return n and 3*sequence(n>>1) + (n&1)

from math import log2

def sequence(i):
    if i == 0:
        return 0
    e = int(log2(i))
    return 3**e + sequence(i - 2**e)

import numpy as np
def sequence(n):
    return ter2dec(dec2bin(n))
    
def dec2bin(n):
    bin = np.array([])
    while n > 0:
        bin = np.append(bin, [n%2])
        n = n//2
    return bin

def ter2dec(ter):
    dec = 0
    for i in range(len(ter)):
        dec += ter[i]*3**i
    return dec

# Induction hypothesis:
# Let S(n) be the sequence of numbers m that don't form arithmetic sequences, such that m < n.
# For a given k, assume that all S(m) is precisely all the numbers m < k without 2's in their ternary form.
# If m has at least one 2 in it's expansion there exist numbers in the sequence with the 2's repalced by 0's and the 2's replaced by 1's.
#     This forms an arithmetic sequence.
# If m has no 2's, consider A and B such that A,B,m is an arithmetic sequence and B is in S(m).
# Comparing the rightmost digits of B and m which are different. A must have a 2 in this position to form an arithmetic sequence.
#     Hence A is not in S(m) and m will be the next number in the sequence.
# 
# Therefore the next number in the sequence has no 2's in it's ternary form.
#
# Base case: S(1) is 0, S(2) is 0,1.
#
# There is a trivial bijection between the nth numbers ternary form and the binary form of n.




def sequence(n):
    # Thx OEIS
    return int(format(n, 'b'), 3)  # Chai Wah Wu, Jan 04 2015

def sequence(n):
    return int(f'{n:b}', 3)

def sequence(n):
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
#Thank SERGEY FOR THIS KATA!!!!!!!!!!!!!!!!!!!!!!!
    return int(bin(n)[2:],3)


def sequence(n):
    return 3 * sequence(n // 2) + n % 2 if n > 0 else 0