예제 #1
0
def digit_expansion(n, prec):
    context = Context(prec=prec + 2)
    sqrt = str(context.sqrt(n))
    total = 0
    #delete last two decimal places to avoid rounding infelicities
    for d in sqrt[:-2]:
        if d != '.':
            total += int(d)
    return total
예제 #2
0
def cont_frac(N):
    context = Context(prec=1000)
    val = Decimal(context.sqrt(N))
    rep = [floor(val)]
    ctr = 1
    while ctr <= 1000:
        val = Decimal(
            context.divide(1, (context.subtract(val, Decimal(floor(val))))))
        rep.append(floor(val))
        ctr += 1
    return rep
예제 #3
0
def cont_frac(N):
    context = Context(prec=1000)
    val = Decimal(context.sqrt(N))
    init_val = floor(val)
    rep = [init_val]
    ctr = 1
    while floor(val) != 2 * init_val:
        val = Decimal(
            context.divide(1, (context.subtract(val, Decimal(floor(val))))))
        rep.append(floor(val))
        ctr += 1
    return rep
예제 #4
0
def fundamental_solution(N):
    context = Context(prec=1000)
    val = Decimal(context.sqrt(N))
    rep = [floor(val)]
    val = Decimal(
        context.divide(1, (context.subtract(val, Decimal(floor(val))))))
    rep.append(floor(val))
    ctr = 1

    h_base = [0, 1]
    k_base = [1, 0]

    h_1 = rep[0] * h_base[-1] + h_base[-2]
    k_1 = rep[0] * k_base[-1] + k_base[-2]

    if check_sol(h_1, k_1, N):
        return h_1, k_1

    h_2 = rep[1] * h_1 + h_base[-1]
    k_2 = rep[1] * k_1 + k_base[-1]

    if check_sol(h_2, k_2, N):
        return h_2, k_2

    H = [h_1, h_2]
    K = [k_1, k_2]

    h = h_2
    k = k_2

    ind = 3
    frac = Fraction(int(h), int(k))

    while not (check_sol(frac.numerator, frac.denominator, N)):

        val = Decimal(
            context.divide(1, (context.subtract(val, Decimal(floor(val))))))
        rep.append(floor(val))
        #val = rep[ind-1]
        h = long(long(floor(val)) * H[-1] + H[-2])
        k = long(long(floor(val)) * K[-1] + K[-2])

        frac = Fraction(long(h), long(k))

        H.append(frac.numerator)
        K.append(frac.denominator)
        ind += 1

    return h, k
예제 #5
0
from decimal import Decimal, Context, ROUND_FLOOR, setcontext

C = Context(rounding=ROUND_FLOOR)
setcontext(C)

x1, y1, x2, y2 = map(Decimal, input().split())
r_2 = (x2 - x1)**2 + (y2 - y1)**2
print(C.sqrt(r_2))
from decimal import Decimal, Context, ROUND_FLOOR, setcontext
from sys import float_info

# NOTE: sqrtする際の誤差をround_floorで切り捨てるので、eで補完する
# ref: https://marco-note.net/abc-191-work-log/
C = Context(rounding=ROUND_FLOOR)
setcontext(C)
# NOTE: pythonの計算機イプシロン
# ref: https://qiita.com/ikuzak/items/1332625192daab208e22
e = Decimal(str(float_info.epsilon))

X, Y, R = map(Decimal, input().split())
zero = Decimal("0")
ans = 0

for x in range(int(X - R), int(X + R) + 1):
    y2_ = R**2 - (X - x)**2
    if y2_ >= zero:
        y_ = C.sqrt(y2_ + e)
        y_min = Y - y_
        y_max = Y + y_
        ans += (int(y_max.quantize(zero)) - int(y_min.quantize(zero)))
print(ans)