Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
#include <pari/pari.h>
2
3
/*
4
GP;install("extgcd", "GG&&", "gcdex", "./libextgcd.so");
5
*/
6
7
/* return d = gcd(a,b), sets u, v such that au + bv = gcd(a,b) */
8
GEN
9
extgcd(GEN A, GEN B, GEN *U, GEN *V)
10
{
11
pari_sp av = avma;
12
GEN ux = gen_1, vx = gen_0, a = A, b = B;
13
14
if (typ(a) != t_INT) pari_err_TYPE("extgcd",a);
15
if (typ(b) != t_INT) pari_err_TYPE("extgcd",b);
16
if (signe(a) < 0) { a = negi(a); ux = negi(ux); }
17
while (!gequal0(b))
18
{
19
GEN r, q = dvmdii(a, b, &r), v = vx;
20
21
vx = subii(ux, mulii(q, vx));
22
ux = v; a = b; b = r;
23
}
24
*U = ux;
25
*V = diviiexact( subii(a, mulii(A,ux)), B );
26
gerepileall(av, 3, &a, U, V); return a;
27
}
28
29
int
30
main()
31
{
32
GEN x, y, d, u, v;
33
pari_init(1000000,2);
34
printf("x = "); x = gp_read_stream(stdin);
35
printf("y = "); y = gp_read_stream(stdin);
36
d = extgcd(x, y, &u, &v);
37
pari_printf("gcd = %Ps\nu = %Ps\nv = %Ps\n", d, u, v);
38
pari_close();
39
return 0;
40
}
41
42