/* This invert algorithm was developed as a model from which to build a c based invert algorithm. Inverter2 is not a satisfactory model because it multiplies and divides without regard to any truncation. This algorith avoids taking any product which cannot be truncated by at most b, the modulus against which the inverse value is sought. Another niceity of this algorithm is its use of array numbers in a mode which closely follows the way a pseudo stack has to be used in the c based program. While bc is reentrant, its returns are single valued. Since the technique for avoiding larger than allowed multiplication requires that both the remainder and quotient be used to pass numbers, the single valued returns of bc had to be supplimented anyway. */ define r(j) { auto u , r r = a[j] - (b[j] % a[j]) x[j] = (b[j] / a[j]) + 1 y[j] = 1 while( b[j] % r > 0) { u = ( b[j] / r ) + 1 x[j] = x[j] * u % b[0] y[j] = (y[j] * u + 1) % b[0] r = r - (b[j] % r) } return(r) } define i(j) { auto r r = r(j) if( r > 1) { a[j + 1] = r b[j + 1] = a[j] i(j + 1) z[j] = (x[j] * z[j + 1] - k[j + 1]) % b[0] k[j] = (z[j + 1] * y[j]) % b[0] return } z[j] = x[j] k[j] = y[j] return } define f(a,b) { j = 0 a[j] = a b[j] = b i(j) return(z[j]) }