.task {
  background: #fff;
  color: #333;
  padding: 25px;
  box-sizing: border-box;
  font-family: Comfortaa, sans-serif !important;
  position: relative;
}
.task code, .task_code {
  display: inline-block;
  padding: 0;
  border-radius: 2px;
  margin: 0;
  position: relative;
  top: 6px;
  background: bisque;
  border: 1px solid #f9d2a4;
  padding: 3px;
  line-height: 1em;
}
.task_code {
  top: -1px;
}
.task_header {
  color: #333 !important;
  margin-top: 0;
  font-size: 30px !important;
}
.task_inner {
  box-shadow: 0 2px 11px -3px rgba(0,0,0,.6);
  padding: 20px;
  border-radius: 3px;
}
.task_devil {
  float: right;
  margin-left: 15px;
  shape-outside: circle(150px);
}
.task_devil img {
  max-width: 150px;
}
.task_part-header { 
  font-size: 20px !important;
  font-weight: 800;
  color: #333;
  padding: 20px 0 10px;
}
.task_part-header:first-of-type {
  padding-top: 0;
}
.task_list {
  padding: 0;
  margin: 10px 0;
  padding-left: 30px;
}
.task_list ul {
  padding: 0;
  margin: 0;
}
.font-effect-canvas-print { color: black; }


Devil's Sequence

Problem



    Robodevil likes to do some mathematics between rehearsals of his orchestra. Today he invented devilish sequence No. 1729:
    

x0 = 0,
x1 = 1,
xn = (xn - 1 + xn - 2) / 2.


    For example, x10 = 0.666015625. Robodevil became interested at once how many `sixes` there were at the beginning of an arbitrary xn. In 6 nanoseconds, he had a formula. Can you do the same?
    Input
    You are given an integer n; `2 ≤ n ≤ 100000`.
    Output
    Output the number of sixes at the beginning of xn in decimal notation.
    Example



Input
Output


10
3

Difficulty: introductory

Starter code:

def count_sixes(n):
	

Solutions:
from math import floor, log
def count_sixes(n):
    return floor((n - n % 2) * log(2, 10))

count_sixes=lambda n:len(str(1<<n-n%2))-1

from math import log10, floor, log

def count_sixes(n):
  k = floor(log10(3) + (n - 1)*log10(2))
  return k - (k*log(10) > (n - (n & 1))*log(2))

# x[n] = 2/3 + d if n is odd
# x[n] = 2/3 - d otherwise
# d = 1/(3*2**(n-1))
# k = log10(1/d)
# adjust k depending on carry/borrow from adding/subtracting d
# if n is odd:
#   k-1 if 1/(3*10**k) <= d
#   k   otherwise
# else:
#   k-1 if 2/(3*10**k) <= d
#   k   otherwise


import math
count_sixes = lambda n: int((n-n%2)*math.log10(2))

def count_sixes(n):
    res = str(10**n//15-(-5)**n//15)
    return len(res)-len(res.lstrip('6'))    

""" For my poor memory :

    with some paper and a pen, 2->10:
        2 : 0.5
        3 : 0.75
        4 : 0.625
        5 : 0.6875
        6 : 0.65625
        7 : 0.671875
        8 : 0.6640625
        9 : 0.66796875
        10 : 0.666015625 ==> that's the example !
        
    I'm lazy : check OEIS with only the decimals (5, 75, 625, 6875...) ==> https://oeis.org/A091903
    Formula : a(n)=10^n/15-(-5)^n/15
    
    Adapt formula to python :)
"""

"""
When you're bad at maths....  x)
The complete story in the comments.
"""


def definer(left, nL, right, nR, name):
    (inpL, outL), (inpR, outR) = left[:2], right[:2]
    return (inpL*nL + inpR*nR,   outL*nL + outR*nR,   left,   nL,   right,   nR,   name)

INF = 10**6

"""
TREE DATA:     length as...      left       nL          right       nR          name (debugging)
               inp     out  
"""
C00          = (4,       0,      None,      None,       None,       None,       "C00")
C2           = (2,       1,      None,      None,       None,       None,       "C2")
C4           = (4,       1,      None,      None,       None,       None,       "C4")

C6           = definer( C2,           1,     C4,         1,     "C6" )
C10          = definer( C4,           1,     C6,         1,     "C10")
C19_6        = definer( C10,         19,     C6,         1,     "C19_6")
C18_6        = definer( C10,         18,     C6,         1,     "C18_6")
C19x5_18x1   = definer( C19_6,        5,     C18_6,      1,     "C19x5_18x1")
C19x4_18x1   = definer( C19_6,        4,     C18_6,      1,     "C19x4_18x1")
C_x5andx4    = definer( C19x5_18x1,   1,     C19x4_18x1, 1,     "C_x5andx4")
C_x13andx4   = definer( C_x5andx4,   13,     C19x4_18x1, 1,     "C_x13andx4")
C_TOP        = definer( C_x13andx4, INF,     C2,         0,     "C_TOP")                # C2 is dummy, here

START        = definer( C00,          1,     C_TOP,      1,     "START" )


def count_sixes(n, c=START):

    _, out, left, nL, right, _, name = c
    
    if left: inpL, outL = left[:2]
    
    if not left: return out
    
    elif n < inpL * nL:
        complete, r  = divmod(n, inpL)
        return outL * complete + (left and count_sixes(r, left))

    else:
        return outL * nL + (right and n and count_sixes(n - inpL*nL, right))      


import math

def count_sixes(n):
    n0 = int((math.log10(2) * (n - 1)))
    n1 = int((math.log10(2) * n))
    n2 = int((math.log10(2) * (n + 1)))
    len = n1
    if n1 == n2 and n1 == (n0 + 1) and (n % 2) != 0:
        len = n0
    return len

from fractions import Fraction 
from decimal import *

def count_sixes(n):
    a = 6 << (n - 2)
    b = Fraction(2,3)
    c = Fraction(1,a) 
    d = 0 
    if n % 2 == 0:
        d = b - c 
    else:
        d = b + c     
    getcontext().prec = 100000
    e = str(Decimal(d.numerator)/Decimal(d.denominator))
    f = 0     
    for i in range(2,len(e)):
        if e[i] == '6': 
            f += 1
        else: break 
    return f


def count_sixes(n):
    num = 3*2**(n-1)
    c=0
    if n%2 ==0 and int(str(num)[:2]) < 15:
        c = 1
    elif n%2 == 1 and int(str(num)[0]) < 3:
        c=1
    return len(str(num))-1 -c