def find_continued_fraction_convergent(numerator, denominator, denominator_threshold):
    """
    Takes in a fraction and expands it into its continued fraction form,
	finding the convergent value with the largest denominator that doesn't
	exceed the provided threshold.

    Parameters:
        numerator (int): The numerator of the input fraction
        denominator (int): The denominator of the input fraction
        denominator_threshold (int): The largest possible value that a covergent's denominator
            is allowed to have in order to be considered a valid result

    Returns:
        The minified (irreducible) fraction representing the largest convergent
	    of the input fraction's continued form that doesn't exceed the given
	    threshold. It is returned as a (numerator, denominator) tuple.
    """

    # Some background on what this step of the algorithm does:
	# Any rational number can be represented by the fraction P/Q, where
	# P and Q are integers. This can also be represented as what's called
	# a continued fraction, which is a number of the form
	# a_0 + 1/(a_1 + 1/(a_2 + 1/(a_3 + ...))).
	# The terms a_i here are called the coefficients.
	# Calculating the convergents is easy with an iterative process:
	# set P_0 = P, Q_0 = Q
	# a_i = P_i / Q_i;		<== Coefficient
	# r_i = P_i % Q_i;		<== Remainder
	# P_i+1 = Q_i;
	# Q_i+1 = r_i;
	# When r_i == 0, this is the last term in the continued fraction.
	# 
	# Continued fractions have things called convergents, which are
	# versions of the fraction but only with i coefficients. Essentially they
	# are closed and closer approximations to the full continued fraction.
	# So if we call
	# v_0 the first convergent, they look like this:
	# v_0 = a_0
	# v_1 = a_0 + 1/a_1
	# v_2 = a_0 + 1/(a_1 + 1/a_2)
	# v_3 = a_0 + 1/(a_1 + 1/(a_2 + 1/a_3)) ...
	# For any convergent, the numerator and denominator will both be integers.
	# The math works out so that for the i-th convergent, you can easily
	# calculate the numerator (n_i) and the denominator (d_i):
	# n_i = a_i * n_(i-1) + n_(i-2)
	# d_i = a_i * d_(i-1) + d_(i-2)
	# 
	# Shor's algorithm finds the convergent of the fraction |X>' / N, where
	# |X>' is the result of the quantum measurement and N = the total
	# number of possible states in the X register, with the largest
	# denominator less than B (the original number being factored).

    coefficient = 0                                     # a_i
    coefficient_calculation_numerator = numerator       # P_i
    coefficient_calculation_denominator = denominator   # Q_i
    coefficient_calculation_remainder = 0               # r_i

    convergent_numerator = 0                            # n_i
    convergent_numerator_1_before = 1                   # n_(i-1)
    convergent_numerator_2_before = 0                   # n_(i-2)

    convergent_denominator = 0                          # d_i
    convergent_denominator_1_before = 0                 # d_(i-1)
    convergent_denominator_2_before = 1                 # d_(i-2)

    while True:
        coefficient = coefficient_calculation_numerator // coefficient_calculation_denominator
        convergent_numerator = coefficient * convergent_numerator_1_before + convergent_numerator_2_before
        convergent_denominator = coefficient * convergent_denominator_1_before + convergent_denominator_2_before

        # We need to calculate this here to check if this was the final term
        coefficient_calculation_remainder = coefficient_calculation_numerator % coefficient_calculation_denominator

        if convergent_denominator > denominator_threshold or coefficient_calculation_remainder == 0:
            if convergent_denominator > denominator_threshold:
                # If the threshold got hit during this iteration, return the previous terms
                convergent_numerator = convergent_numerator_1_before
                convergent_denominator = convergent_denominator_1_before
            return (convergent_numerator, convergent_denominator)
        else:
            # Calculate the terms that will be used for the next round
            coefficient_calculation_numerator = coefficient_calculation_denominator
            coefficient_calculation_denominator = coefficient_calculation_remainder
            convergent_numerator_2_before = convergent_numerator_1_before
            convergent_numerator_1_before = convergent_numerator
            convergent_denominator_2_before = convergent_denominator_1_before
            convergent_denominator_1_before = convergent_denominator