editing
approved
editing
approved
(PARI) /* Define operators for numbers represented (recursively) as list of positions of bits 1, as illustrated by . Illustration using the next 2 commands, e.g. below: T = 3.bits; T.int */
A2845 = List([[2.bits]]) /* list List of values for each n */
{A002845(n)= while(#A2845<n, my(S=[], n=#A2845); for(k=1, n, foreach(A2845[n-k+1], b, S=setunion(S, Set([POW(a, b)| a<-A2845[k]])))); listput(A2845, S)); #A2845[n]}
approved
editing
editing
approved
{SUB(m, n, a=#n)= if(!a, m; , my(b=a=1, c, i); while(a<=#m && b<=#n, if(0>c=CMP(m[a], n[b]), a++, c, i=[c=n[b]]; b++; while(m[a]!=c=ADD(c, ONE), if(b<=#n && c==n[b], b++, i=concat(i, [c]))); m=concat([m[1..a-1], i, m[a+1..#m]]); a += #i, m=m[^a]; b++)); m)}
approved
editing
editing
approved
{POW(m, n)= if(#m==1, [MUL(m[1], n)], my(p=ONE); until(!#n || !#m=MUL(m, m), #n[1] || p=MUL(p, m); n=RSHIFT(n)); p)} /* \\ binary exponentiation unless only 1 bit set */
{RSHIFT(m, n=ONE)= if(!#m|| !#n|| !(#m[1]|| #m=m[^1]), m, my(go); [SUB(b, n)| b<-m, go||(CMP(b, n)>=0 && go=1)])}
{ADD(m, n, a=#m, b=#n)= if(!a, n, !b, m, a>b && [m, n]=[n, m]; a=b=1; until( a>#m || b>#n, if(m[a]==n[b], until(a>=#m|| m[a]!=m[a+1]|| !#m=m[^a], m[a]=ADD(m[a], ONE)); b++, CMP(m[a], n[b]) < 0, a++, m=concat([m[1..a-1], [n[b]], m[a..#m]]); b++)); b>#n|| m=concat(m, n[b..#n]); m)}
{SUB(m, n, a=#n)= if(!a, m; my(b=a=1, c, insi); while(a<=#m && b<=#n, if(0>c=cmpCMP(m[a], n[b]), a++, c, insi=[c=n[b]]; b++; while(m[a]!=c=ADD(c, ONE), if(c==n[b], b++, insi=concat(ins, i, [c]))); m=concat([m[1..a-1], ins, i, m[a+1 .. #m]]); a += #ins, i, m=m[^a]; b++)); m)}
A2845=List([[2.bits]] ) /* list of values for each n */
{A002845(n)=while(#A2845<n, my(S=[], n=#A2845); for(k=1, n, foreach(A2845[n-k+1], b, S= setunion(S, Set([POW(a, b)| a<-A2845[k]])))); A2845=concatlistput(A2845, [S])); #A2845[n]}
approved
editing
proposed
approved
editing
proposed
n.bits = vector(hammingweight(n), v, n -= 1 << v= valuation(n, 2); v.bits)
ONE = 1.bits; m.int = sum(i=1, #m, 1<<m[i].int) /* Convert back. (Not needed.) */
{MUL(m, n)= my(S=[]); #n && foreach(m, b, S=ADD(S, LSHIFT[ADD(n, c, b)| c<-n])); S}
{LSHIFTADD(m, n, a=#m, b=ONE#n)= if(!a, n, !b, m, a>b && [m, n]=[n, m]; a=b=1; until( a>#m || b>#n, if(m[a]==n[b], until(a>=#m|| m[a]!=m[a+1]|| !#m=m[^a], m, [a]=ADD(m[a], ONE)); b, ++, CMP(m[a], n)| [b]) < 0, a++, m=concat([m[1..a-1], [n[b]], m[a..#m]]); b++)); b>#n|| m=concat(m, n[b..#n]); m)}
{CMP(m, n, a=#m, b=#n, c=0)= if(!b, a, !a, -1, while(!(c=CMP(m[a], n[b]))&& a--&& b--, ); if(c, c, 1-b))}
{ADDSUB(m, n, a=#n)= if(!#m, n, !#n, m, #m>#n && [m, n]=[n, a, m]; my(a=1, b=a=1, c, ins); untilwhile( a><=#m || && b><=#n, if(0>c=cmp(m[a]==, n[b], until(), a>++, c, ins=#m|| m[a]!c=mn[a+1b]|| !#m=m[^a], ; b++; while(m[a]!=c=ADD(m[a], c, ONE)); b++, CMP, if(m[a], c==n[b]) < 0, a, b++, ins=concat(ins, [c]))); m=concat([m[1..a-1], [n[b]], ins, m[a+1 .. #m]]); b+a +)); b>= #n|| ins, m=concat(m, n[^a]; b..#n]++)); m)}
{CMP(m, n)= if(!#m, -!!#n, !#n, 1, my(a=#m, b=#n, c); while(!(c = CMP(m[a], n[b])) && a-- && b--, ); if(c, c, 1-b))}
{SUB(m, n)= #n||return m; my(a=1, b=1, c); while( a<=#m && b<=#n, if( !c=cmp(m[a], n[b]), m=m[^a]; b++, c<0, a++, c=n[b]; b++; my(ins=[c]); while(m[a] != c=ADD(c, ONE), if(c==n[b], b++, ins=concat(ins, [c])))/*while*/; m=concat([m[1..a-1], ins, m[a+1 .. #m]]); a += #ins))/*end if !c, while a…*/; m}
proposed
editing
editing
proposed