Problem p03329 (python)

Fixed submission:
#!/usr/bin/env python3
import math
# n進数の各位の和


def base_10_n(x, n):
    if x == 0:
        return 0
    tmpx = x
    a = int(math.log(x, n))
    ans = 0
    for i in range(a, -1, -1):
        ans += tmpx // n**i
        tmpx %= n**i
    ans += tmpx
    return ans


def s(n, r):
    if n == 0:
        return 0
    return n % r + s(n // r, r)


def main():
    N = int(input())
    ans = 10**12
    for j in range(N+1):
        # c = base_10_n(N-j, 6)
        # d = base_10_n(j, 9)
        c = s(j,6)
        d = s(N-j,9)
        ans = min(ans, c+d)
    print(ans)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math
# n進数の各位の和
def base_10_n(x,n):
    tmpx = x
    a = int(math.log(x,n))
    ans = 0
    for i in range(a,-1,-1):
        ans += tmpx // n**i
        tmpx %= n**i
    ans += tmpx
    return ans

def main():
    N = int(input())
    ans = base_10_n(N,6)
    for j in range(1, (N//9)+1):
        c = base_10_n(N-9*j,6)
        d = base_10_n(9*j,9)
        ans = min(ans, c+d)
    print(ans)
    # print(base_10_n(-1,6))
    # print([i for i in range(1,(26//9)+1)])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math
# n進数の各位の和
def base_10_n(x,n):
    a = int(math.log(x,n))
    ans = 0
    for i in range(a,-1,-1):
        ans += x // n**i
        x %= n**i
    ans += x
    return ans

def main():
    N = int(input())
    ans = base_10_n(N,6)
    for j in range(1, (N//9)+1):
        c = base_10_n(N-9*j,6)
        d = base_10_n(9*j,9)
        ans = min(ans, c+d)
    print(ans)
    # print(base_10_n(-1,6))
    # print([i for i in range(2,-1,-1)])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math
# n進数の各位の和
def base_10_n(x,n):
    a = int(math.log(x,n))
    ans = 0
    for i in range(a,-1,-1):
        ans += x // n**i
        x %= n**i
    ans += x
    return ans

def main():
    N = int(input())
    ans = base_10_n(N,6)
    for j in range(1,N//18 + 1):
        c = base_10_n(N-18*j,6)
        d = base_10_n(18*j,9)
        ans = min(ans, c+d)
    print(ans)
    # print(int(math.log(85,9)))
    # print([i for i in range(2,-1,-1)])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math
# n進数の各位の和
def base_10_n(x,n):
    a = int(math.log(x,n))
    ans = 0
    for i in range(a,-1,-1):
        ans += x // n**i
        x %= n**i
    ans += x
    return ans

def main():
    N = int(input())
    ans = N
    for j in range(1,N//18 + 1):
        a = base_10_n(N-18*j,6)
        b = base_10_n(18*j,9)
        ans = min(ans, a+b)
    print(ans)
    # print([i for i in range(2,-1,-1)])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math
# n進数の各位の和
def base_10_n(x,n):
    a = int(math.log(x,n))
    ans = 0
    for i in range(a+1,0,-1):
        ans += x // n**i
        x %= n**i
    ans += x
    return ans

def main():
    N = int(input())
    ans = N
    for j in range(1,N//18 + 1):
        a = base_10_n(N-18*j,6)
        b = base_10_n(18*j,9)
        # print(18*j,a,b)
        ans = min(ans, a+b)
    print(ans)

if __name__ == "__main__":
    main()