Project Euler #66: Diophantine equation

Sort by

recency

|

12 Discussions

|

  • + 1 comment

    I'll summarize the Wikipedia/Wolfram info

    1. The minimum solution is the numerator of a continued fraction aproximation of sqrt(D)
    2. let p be the period of the continued fraction and n_i / d_i be the ith convergent in the sequence of continued fraction aproximations of sqrt(D)
    3. if D is even, the solution is n_(p-1)
    4. if D is odd, the solution is n_(2p-1)
    5. Project Euler+ problems 64 and 65 lay the groundwork for this problem
  • + 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)
    
  • + 0 comments

    Is there a way for you to see the time taken by your code in each test case? I am a noobie and I like to know how fast the computer can crunch the numbers.

  • + 0 comments

    I finally got my solution to work. It was taking too long on the last two cases. Turns out that manual calculation was faster than using python's fractions library.

  • + 1 comment

    Wikipedia has a long article about this problem: https://en.wikipedia.org/wiki/Pell%27s_equation