bigint* copybigint(bigint* from, bigint* to) { int n = abs(from->size); digit *f, *t; if (to==NULL) to = mkbigint(n); else if (to->allocsize<n) { freemem(to); to=mkbigint(n); } to->size = from->size; for (f=from->data, t=to->data; n>0; --n) *t++=*f++; return to; }
object Factor(bigint* num) { num=copybigint(num,NULL); if (num->size<0) { Printf("- "); num->size=-num->size; } { bigint* temp=mkbigint(num->size); _digit p; int i=0; if (num->size==0) { Printf("0"); goto quit; } for (p=2; p<=trial_limit; p+= inc[i++]) { if (i==array_size(inc)) i=3; /* after |37-31| wrap to difference |11-7| */ if (copybigint(num,temp),div1(temp,p)==0) { _index n; _digit pn=p; int e=1; copybigint(temp,num); for (n=1; pn<=MaxDigit/p; ++n) pn*=p; /* highest $p^n$ fitting in |_digit| */ for (; div1(temp,pn)==0; e+=n) copybigint(temp,num); /* find factors $p^n$ */ if (n>1) /* then there might be some factors |p| left */ for (copybigint(num,temp); div1(temp,p)==0; ++e) copybigint(temp,num); /* factors |p| */ Printf("%ld",(long)p); if (e>1) Printf("^%ld",(long)e); if (cmp1(num,1)==0) goto quit; /* last factor was found */ Printf(" * "); } } printbigint(num,0); if (num->size>2) Printf(" (Last factor need not be a prime)"); quit: Printf("\n"); freemem(num); freemem(temp); } return (object) NULL; }
pointer ICONVOPEN(context *ctx, int n, pointer *argv) { int cd; ckarg(2); if (!isstring(argv[0])) error (E_NOSTRING, argv[0]); if (!isstring(argv[1])) error (E_NOSTRING, argv[1]); cd=iconv_open(argv[0]->c.str.chars, argv[1]->c.str.chars); return(mkbigint(cd));}