void qsub(void) { unsigned int *a, *ab, *b, *ba, *c; save(); p2 = pop(); p1 = pop(); ab = mmul(p1->u.q.a, p2->u.q.b); ba = mmul(p1->u.q.b, p2->u.q.a); a = msub(ab, ba); mfree(ab); mfree(ba); // zero? if (MZERO(a)) { mfree(a); push(zero); restore(); return; } b = mmul(p1->u.q.b, p2->u.q.b); c = mgcd(a, b); MSIGN(c) = MSIGN(b); p1 = alloc(); p1->k = NUM; p1->u.q.a = mdiv(a, c); p1->u.q.b = mdiv(b, c); mfree(a); mfree(b); mfree(c); push(p1); restore(); }
int main (int argc, char *argv[]) { MINT *a, *b, *c, *d; short h; mp_set_memory_functions (NULL, NULL, NULL); a = itom (123); b = xtom ("DEADBEEF"); c = itom (0); d = itom (0); move (a, b); madd (a, b, c); msub (a, b, c); mult (a, b, c); mdiv (b, a, c, d); sdiv (b, 2, c, &h); msqrt (a, c, d); pow (b, a, a, c); rpow (a, 3, c); gcd (a, b, c); mcmp (a, b); if (argc > 1) { min (c); mout (a); } mtox (b); mfree(a); exit (0); }
void mpolunit(MPOL *p, MINT *coef, MPOL *q) { register i; MINT *resco,rem; MPOL res; if (p->nterms==0) { mpolfree(q); mset(0,coef); return; }; MINIT(&rem); MFREE(coef); MCOPY(&(p->coefs[0]),coef); for (i=1;i<p->nterms;i++) mgcd(coef,&(p->coefs[i]),coef); MPOLINIT(&res); res.nterms = p->nterms; POL_ALLOC(&res,p->nterms); resco = res.coefs; for (i=0;i<p->nterms;i++){ MINIT(resco); mdiv(&(p->coefs[i]),coef,(resco++),&rem); expocopy(MEXPO(p,i),MEXPO(&res,i)); } mpolfree(q); MPOLMOVEFREE(&res,q); MFREE(&rem); }
static A jttayamp(J jt,A w,B nf,A x,A h){A y;B ng=!nf;I j,n;V*v=VAV(h); ASSERT(AR(x)<=(nf?v->lr:v->rr),EVRANK); switch(v->id){ case CPLUS: R tpoly(over(x,one)); case CMINUS: R tpoly(nf?over(x,num[-1]):over(negate(x),one)); case CSTAR: R tpoly(over(zero,x)); case CDIV: ASSERT(ng,EVDOMAIN); R tpoly(over(zero,recip(x))); case CJDOT: R tpoly(nf?over(x,a0j1):over(jdot1(x),one)); case CPOLY: ASSERT(nf,EVDOMAIN); R tpoly(BOX&AT(x)?poly1(x):x); case CHGEOM: ASSERT(nf,EVDOMAIN); RE(j=i0(x)); ASSERT(0<=j,EVDOMAIN); y=IX(j); R tpoly(divide(hgcoeff(y,h),fact(y))); case CBANG: ASSERT(nf,EVDOMAIN); RE(j=i0(x)); ASSERT(0<=j,EVDOMAIN); R tpoly(divide(poly1(box(iota(x))),fact(x))); case CEXP: if(nf)R eva(x,"(^.x)&^ % !"); RE(n=i0(x)); R 0<=n?tpoly(over(reshape(x,zero),one)):atop(ds(CDIV),amp(h,sc(-n))); case CFIT: ASSERT(nf&&CPOLY==ID(v->f),EVDOMAIN); y=over(x,IX(IC(x))); R tpoly(mdiv(df2(x,y,h),atab(CEXP,y,IX(IC(x))))); case CCIRCLE: switch(i0(x)){ case 1: R eval("{&0 1 0 _1@(4&|) % !"); case -3: R eval("{&0 1 0 _1@(4&|) % ]"); case 2: R eval("{&1 0 _1 0@(4&|) % !"); case 5: R eval("2&| % !"); case -7: R eval("2&| % ]"); case 6: R eval("2&|@>: % !"); case -1: R eval("(2&| % ]) * ([: */ (1&+ % 2&+)@(i.@<.&.-:))\"0"); case -5: R eval("({&0 1 0 _1@(4&|) % ]) * ([: */ (1&+ % 2&+)@(i.@<.&.-:))\"0"); }} ASSERT(0,EVDOMAIN); }
BigInt *balance(BigInt *num) { //makes sure every entry in digits is a digit int length = num->length; int *digits = num->digits; int sign = num->sign; int i=0; int needs_repeating = 0; for(i=0; i<length-1;++i) { digits[i+1] += mdiv(digits[i], 10); digits[i] = mmod(digits[i], 10); } if (digits[length-1] >= 10) { int a = mdiv(digits[length-1], 10); int b = mmod(digits[length-1], 10); digits[length] = a; digits[length-1] = b; ++length; } if(digits[length-1]<0) { digits[length-1] *= -1; sign *= -1; //perform k*10^length - rest = (k-1)*10^length + 999..99 - rest + 1 digits[length-1] -= 1; for(i=0; i<length-1; ++i) { digits[i] = 9 - digits[i]; } digits[0] += 1; //may cause digits[0] to become unbalanced again needs_repeating = 1; } num->digits = digits; num->length = length; num->sign = sign; num->size = num->size; if (needs_repeating) {num = balance(num);} //won't have the same problem again if(!strcmp(toString(num), "0")) { num->sign = 0; } return num; //TODO add 0 }
FN minvert(MINT *a, MINT *b, MINT *c) { MINT x, y, z, w, Anew, Aold; int i = 0; static MINT one; static int oneinit = 1; if (oneinit) { oneinit = 0; MSET(1,&one); } MINIT(&x); MINIT(&y); MINIT(&z); MINIT(&w); MINIT(&Aold); MSET (1,&Anew); mcopy(b, &x); mcopy(a, &y); /* * Loop invariant: * * y = -1^i * Anew * a mod b */ while(mtest(&y) != 0) { mdiv(&x, &y, &w, &z); mcopy(&Anew, &x); mmult(&w, &Anew, &Anew); madd(&Anew, &Aold, &Anew); mmove(&x, &Aold); mmove(&y, &x); mmove(&z, &y); i++; } if (mcmp(&one,&x)) { mcopy(&one,c); } else { mmove(&Aold, c); if( (i&01) == 0) msub(b, c, c); } MFREE(&x); MFREE(&y); MFREE(&z); MFREE(&w); MFREE(&Aold); MFREE(&Anew); }
main(void) { MINT a,b,q,r; MINIT(&a); MINIT(&b); MINIT(&q); MINIT(&r); while ((fputs("a: ",stdout), min(&a) != EOF) && (fputs("b: ",stdout), min(&b) != EOF)) { mdiv(&a,&b,&q,&r); mout(&a); putchar('\n'); mout(&b); putchar('\n'); mout(&q); putchar('\n'); mout(&r); putchar('\n'); mmod(&a,&b,&r); mout(&a); putchar('\n'); mout(&b); putchar('\n'); mout(&r); putchar('\n'); } exit(0); }
void mgcd(MINT *a, MINT *b, MINT *c) { MINT x,y,z,w; MCOPY(a,&x); MCOPY(b,&y); MINIT(&z); MINIT(&w); while( mtest(&y) != 0) { mdiv(&x,&y,&w,&z); mmove(&y,&x); mmove(&z,&y); #if (! INTR) (*PollPtr)(); #endif } mmove(&x,c); MFREE(&y); MFREE(&z); MFREE(&w); return; }
void bignum_truncate(void) { unsigned int *a; save(); p1 = pop(); a = mdiv(p1->u.q.a, p1->u.q.b); p1 = alloc(); p1->k = NUM; p1->u.q.a = a; p1->u.q.b = mint(1); push(p1); restore(); }
void yyfloor(void) { double d; p1 = pop(); if (!isnum(p1)) { push_symbol(FLOOR); push(p1); list(2); return; } if (isdouble(p1)) { d = floor(p1->u.d); push_double(d); return; } if (isinteger(p1)) { push(p1); return; } p3 = alloc(); p3->k = NUM; p3->u.q.a = mdiv(p1->u.q.a, p1->u.q.b); p3->u.q.b = mint(1); push(p3); if (isnegativenumber(p1)) { push_integer(-1); add(); } }
msqrt(MINT *a, MINT *b, MINT *r) { MINT x,y,z; register alen,j; MINIT(&x); MINIT(&y); MINIT(&z); alen = a->len; if (alen<0) mpfatal("msqrt: neg arg"); if (alen==0) { mset(0,b); mset(0,r); return(0); } if(alen & 01) x.len = (1+alen)/2; else x.len = 1 + alen/2; valloc(x.val,x.len); for (j=x.len; (--j)>=0;) x.val[j]=0; if (alen & 01) x.val[x.len-1]=0400; else x.val[x.len-1]=1; for (;;) { mdiv(a,&x,&y,&z); madd(&x,&y,&y); mshiftr(&y,1); if (mcmp(&x,&y) <= 0) break; mmove(&y,&x); } mcopy(&x,&y); mmult(&x,&x,&x); msub(a,&x,r); MFREE(&x); MMOVEFREE(&y,b); MFREE(&z); return(r->len); }
main() { int i; char *p; uid = getuid(); myname = getlogin(); if(myname == NULL) myname = getpwuid(uid)->pw_name; comminit(); mbuf = itom(0); files(); setup(getpass("Key: ")); mkb(); mkx(); #ifndef debug invert(x, b, x); #else invert(x, b, z); mult(x, z, z); mdiv(z, b, q, z); omout(z); invert(x, b, x); #endif for(i=0; i<fcnt; i++) { sprintf(line, "%s%s.%d", maildir, myname, fnum[i]); if(stat(line, &stbuf)<0) { perror(line); continue; } if(stbuf.st_size == 0) { printf("zero length mail file\n"); unlink(line); continue; } if((mf = fopen(line, "r"))==NULL) { perror(line); continue; } decipher(mf, stdout); cmnd: printf("? "); fgets(buf, sizeof(buf), stdin); if(feof(stdin)) exit(0); switch(buf[0]) { case 'q': exit(0); case 'n': case 'd': case '\n': unlink(line); fclose(mf); break; case '!': system(buf+1); printf("!\n"); goto cmnd; case 's': case 'w': rewind(mf); if(buf[1] == '\n' || buf[1] == '\0') strcpy(buf, "s mbox\n"); for(p=buf; !isspace(*p); p++); for(; isspace(*p); p++); p[strlen(p)-1] = 0; kf = fopen(p, "a"); if(kf == NULL) { perror(p); break; } decipher(mf, kf); fclose(mf); fclose(kf); unlink(line); break; } } exit(0); }
main() { int i; char *p; uid = getuid(); myname = (char *)getlogin(); if(myname == NULL) myname = getpwuid(uid)->pw_name; comminit(); mbuf = itom(0); files(); setup(getpass("Key: ")); mkb(); mkx(); #ifndef debug invert(x, b, x); #else invert(x, b, z); mult(x, z, z); mdiv(z, b, q, z); omout(z); invert(x, b, x); #endif for(i=0; i<fcnt; i++) { sprintf(line, "%s%s.%d", maildir, myname, fnum[i]); if(stat(line, &stbuf)<0) { perror(line); continue; } if(stbuf.st_size == 0) { printf("zero length mail file\n"); unlink(line); continue; } if((mf = fopen(line, "r"))==NULL) { perror(line); continue; } decipher(mf, stdout); cmnd: printf("? "); fgets(buf, sizeof(buf), stdin); if(feof(stdin)) exit(0); switch(buf[0]) { case 'q': exit(0); case 'n': case 'd': case '\n': fclose(mf); unlink(line); break; case '!': system(buf+1); printf("!\n"); goto cmnd; case 's': case 'w': rewind(mf); if(buf[1] == '\n' || buf[1] == '\0') strcpy(buf, "s mbox\n"); for(p = buf+1; isspace(*p); p++); p[strlen(p)-1] = 0; kf = fopen(p, "a"); if(kf == NULL) { perror(p); goto cmnd; } decipher(mf, kf); fclose(mf); fclose(kf); unlink(line); break; default: printf("Commands are:\n"); printf("q quit, leaving unread messages\n"); printf("n delete current message and goto next\n"); printf("d same as above\n"); printf("\\n same as above\n"); printf("! execute shell command\n"); printf("s save message in the named file or mbox\n"); printf("w same as above\n"); printf("? prints this list\n"); goto cmnd; } } exit(0); }
unsigned int * mfactor(unsigned int *n) { unsigned int *r, *root, *t, *two, *x, *y; two = mint(2); root = msqrt(n); // y = 1; y = mint(1); // x = 2 isqrt(n) + 1 t = madd(root, root); x = madd(t, y); mfree(t); // r = isqrt(n) ^ 2 - n t = mmul(root, root); r = msub(t, n); mfree(t); mfree(root); while (1) { if (MZERO(r)) { // n = (x - y) / 2 t = msub(x, y); n = mdiv(t, two); mfree(t); mfree(r); mfree(x); mfree(y); mfree(two); return n; } // r = r + x t = madd(r, x); mfree(r); r = t; // x = x + 2 t = madd(x, two); mfree(x); x = t; while (1) { // r = r - y t = msub(r, y); mfree(r); r = t; // y = y + 2 t = madd(y, two); mfree(y); y = t; if (MSIGN(r) == -1 || MZERO(r)) break; } } }