Project Euler #66: Diophantine equation

  • + 0 comments

    here is my python 3 100/- Point Solution

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import math
    
    def is_square(n):
        sqrt_n = int(math.sqrt(n))
        return sqrt_n * sqrt_n == n
    
    def minimal_solution(D):
        m, d, a = 0, 1, int(math.sqrt(D))
        num1, num2 = 1, a
        den1, den2 = 0, 1
    
        while num2*num2 - D*den2*den2 != 1:
            m = d*a - m
            d = (D - m*m) // d
            a = (int(math.sqrt(D)) + m) // d
    
            num1, num2 = num2, a*num2 + num1
            den1, den2 = den2, a*den2 + den1
    
        return num2
    
    def largest_minimal_solution(limit):
        max_D = 0
        max_minimal_solution = 0
    
        for D in range(2, limit + 1):
            if not is_square(D):
                solution = minimal_solution(D)
                if solution > max_minimal_solution:
                    max_minimal_solution = solution
                    max_D = D
    
        return max_D
    
    # Read input and calculate the result
    limit = int(input())
    result = largest_minimal_solution(limit)
    print(result)