def PQa(P_0, Q_0, D):
 A_i_2 = B_i_1 = 0
 A_i_1 = B_i_2 = 1
 G_i_2 = (- P_0)
 G_i_1 = Q_0
 P_i = P_0
 Q_i = Q_0
 while 1:
  a_i = floor(((P_i + sqrt(D)) / Q_i))
  A_i = ((a_i * A_i_1) + A_i_2)
  B_i = ((a_i * B_i_1) + B_i_2)
  G_i = ((a_i * G_i_1) + G_i_2)
  (yield (P_i, Q_i, a_i, A_i, B_i, G_i))
  (A_i_1, A_i_2) = (A_i, A_i_1)
  (B_i_1, B_i_2) = (B_i, B_i_1)
  (G_i_1, G_i_2) = (G_i, G_i_1)
  P_i = ((a_i * Q_i) - P_i)
  Q_i = ((D - (P_i ** 2)) / Q_i)

def PQa ( P_0 , Q_0 , D ) : A_i_2 = B_i_1 = 0 A_i_1 = B_i_2 = 1 G_i_2 = ( - P_0 ) G_i_1 = Q_0 P_i = P_0 Q_i = Q_0 while 1 : a_i = floor ( ( ( P_i + sqrt ( D ) ) / Q_i ) ) A_i = ( ( a_i * A_i_1 ) + A_i_2 ) B_i = ( ( a_i * B_i_1 ) + B_i_2 ) G_i = ( ( a_i * G_i_1 ) + G_i_2 ) ( yield ( P_i , Q_i , a_i , A_i , B_i , G_i ) ) ( A_i_1 , A_i_2 ) = ( A_i , A_i_1 ) ( B_i_1 , B_i_2 ) = ( B_i , B_i_1 ) ( G_i_1 , G_i_2 ) = ( G_i , G_i_1 ) P_i = ( ( a_i * Q_i ) - P_i ) Q_i = ( ( D - ( P_i ** 2 ) ) / Q_i )
returns useful information needed to solve the pell equation .