Beispiel #1
0
def answer():
    count = 0
    for n in range(2, 10001):
        if is_square(n):
            continue
        if len(square_continued(n))%2:
            count += 1
    return count
Beispiel #2
0
def solve_pells(num):
    if is_square(num):
        return 0

    # b_n is found by square_continued and a_n is always 1
    guess = int(num**0.5)
    b = square_continued(num)
    #A_1 and B_1 given by A_1/B_1 = ((b1*b0) + a1)/b1
    numerator = [guess, guess*b[0] + 1]
    denominator = [1, b[0]]
    if numerator[1]**2 - num*denominator[1]**2 == 1:
        return numerator[1]
    for n in itertools.count(2):
        # A_n = b_n * A_n-1 + a_n* A_n-2 where A_n is approx numerator
        # B_n = b_n * B_n-1 + a_n* A_n-2 where B_n is approx denominator
        x = b[(n-1)%len(b)] * numerator[n-1] + numerator[n-2]
        y = b[(n-1)%len(b)] * denominator[n-1] + denominator[n-2]
        if x**2 - num*y**2 == 1:
            return x 
        numerator.append(x)
        denominator.append(y)