/usr/share/doc/pari-gp/examples/extgcd.c is in pari-gp 2.5.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <pari/pari.h>
/*
GP;install("extgcd", "GG&&", "gcdex", "./libextgcd.so");
*/
/* return d = gcd(a,b), sets u, v such that au + bv = gcd(a,b) */
GEN
extgcd(GEN A, GEN B, GEN *U, GEN *V)
{
pari_sp av = avma;
GEN ux = gen_1, vx = gen_0, a = A, b = B;
if (typ(a) != t_INT || typ(b) != t_INT) pari_err(typeer, "extgcd");
if (signe(a) < 0) { a = negi(a); ux = negi(ux); }
while (!gequal0(b))
{
GEN r, q = dvmdii(a, b, &r), v = vx;
vx = subii(ux, mulii(q, vx));
ux = v; a = b; b = r;
}
*U = ux;
*V = diviiexact( subii(a, mulii(A,ux)), B );
gerepileall(av, 3, &a, U, V); return a;
}
int
main()
{
GEN x, y, d, u, v;
pari_init(1000000,2);
printf("x = "); x = gp_read_stream(stdin);
printf("y = "); y = gp_read_stream(stdin);
d = extgcd(x, y, &u, &v);
pari_printf("gcd = %Ps\nu = %Ps\nv = %Ps\n", d, u, v);
pari_close();
return 0;
}
|