orangesquid (os) wrote,
orangesquid
os

pseudo-base-prime part...5?

More base-prime stuff.  I extended the bc code; there are function names matching the HP User-RPL programs, and the size of the array is stored as element 0
(Excluding a row/column pair; then it's the row in element 0 and the column in element 1)

The num2p and p2num routines are as before.
---snip---
/* example usage: */
/* j=num2p(200,f[]) --> 3 0 2 in f[] */
/* n=p2num(f[],j) returns 200 */

define num2p(n, *f[]) {
  auto i, j, l, s[], z;
  z = scale;
  scale = 0;
  l = n;
  for(i = 2; i <= l; ++i) {
    s[i - 2] = 1;
  }
  for(i = 2; i <= l; ++i) {
    for(j = 2; i*j <= l; ++j) {
      s[i*j - 2] = 0;
    }
  }
  for(i = 2; i <= l; ++i) {
    print i, ": ", s[i - 2], "\n";
  }
  j = 0;
  for(i = 2; i <= l; ++i) {
    if(s[i - 2]) {
      f[j] = 0;
      print n, " % ", i, " == ", n % i;
      while(n % i == 0) {
        ++f[j];
        n = n / i;
        print "for ", i, " (p#", j, "): n -> ", n, "; f -> ", f[j], "\n";
      }
      ++j;
    }
  }
  for(i = 0; i < j; ++i) {
    print f[i], " "
  }
  print "\n"
  scale = z;
  return j;
}

define void basep(n, *f[]) {
  auto j, g[], i;
  j = num2p(n,g[]);
  for(i = 0; i < j; ++i) {
    f[i+1] = g[i];
  }
  f[0] = j;
  bpnorm(f[]);
}

define bp2num(f[]) {
  auto i, p[], r, n;
  r=f[0];
  for(i = 0; i < r; ++i) {
    p[i] = f[i + 1];
  }
  n = p2num(p[],r);
  return n;
}

define p2num(p[], r) {
  auto i, j, l, s[], z, o, k;
  z = scale;
  scale = 0;
  l = r;
  k = 0;
  while(k < r) {
    l = l * 2;
    for(i = 2; i <= l; ++i) {
      s[i - 2] = 1;
    }
    for(i = 2; i <= l; ++i) {
      for(j = 2; i*j <= l; ++j) {
        s[i*j - 2] = 0;
      }
    }
    k = 0;
    for(i = 2; i <= l; ++i) {
      if(s[i - 2]) {
        ++k;
      }
    }
  }
  print "l=", l, " k=", k, "\n";
  for(i = 2; i <= l; ++i) {
    print i, ": ", s[i - 2], "\n";
  }
  o = 1;
  j = 0;
  for(i = 2; i <= l; ++i) {
    if(s[i - 2]) {
      if(p[j]) {
        o = o * i^p[j];
        print "after ", i, " (p#", j, "): o -> ", o, " from p:", p[j], "\n";
      }
      ++j;
    }
  }
  scale = z;
  return o;
}

define squid(a,b) {
  auto m, n, p[], q[], r[], j, k, l;
  j = num2p(a,p[]);
  k = num2p(b,q[]);
  l = j + k;
  print "j:", j, " k:", k, " -> l:", l, "\n";
  for(m = 0; m < l; ++m) {
    r[m] = 0;
  }
  for(m = 0; m < j; ++m) {
    for(n = 0; n < k; ++n) {
      r[m + n] = r[m + n] + p[m]*q[n];
      print "@", m, ",", n, " r->", r[m+n], " from ", p[m], ",", q[n], "\n";
    }
  }
  return p2num(r[],l);
}

define void bpconj(p[],*q[]) {
  auto i;
  if(p[0] == 0) {
    q[0] = 1;
    q[1] = 1;
    return;
  }
  if(p[1] == 0) {
    for(i = 2; i <= p[0]; ++i) {
      q[i - 1] = p[i];
    }
    q[0] = p[0] - 1;
    q[1] = q[1] + 1;
  } else {
    for(i = 1; i <= p[0]; ++i) {
      q[i + 1] = p[i];
    }
    q[1] = 0;
    q[0] = p[0] + 1;
    q[2] = q[2] - 1;
  }
}

define void bpnorm(*p[]) {
  auto i, c, b;
  b=0;
  for(i = p[0]; i > 0; --i) {
    if(b == 0) {
      if(p[i] == 0) {
        c++;
      } else {
        b++;
      }
    }
  }
  p[0] = p[0] - c;
}

define void disp(p[]) {
  auto i;
  print "{";
  for(i = 1; i <= p[0]; ++i) {
    print p[i];
    if(i < p[0]) {
      print ",";
    }
  }
  print "}\n";
}

define void bptree(r,c,*p[]) {
  auto i, j, s, m, o[], z;
  s = scale;
  scale = 0;
  p[0] = 1;
  p[1] = 0;
  for(i = 0; i < r; ++i) {
    print "@r", i, ":";
    m = c / 2;
    m = m * 2;
    m = c - m;
    if(m > 0) {
      print "R";
      o[r - i - 1] = 0;
      c = (c - 1) / 2;
    } else {
      print "L";
      o[r - i - 1] = 1;
      c = c / 2;
    }
    print ",c:=", c, "\n";
    disp(p[]);
  }
  for(i = 0; i < r; ++i) {
    print "op", i, ":", o[i], "\n";
    if(o[i] > 0) {
      p[1] = p[1] + 1;
    } else {
      print "n=", p[0], ":";
      disp(p[]);
      for(j = p[0]; j >= 1; --j) {
        print "j.", j, ".", p[j], "\n";
        p[j + 1] = p[j];
      }
      p[1] = 0;
      p[0] = p[0] + 1;
    }
    disp(p[]);
  }
  scale = s;
}

define void inbptree(p[],*rc[]) {
  auto g, r, c, i, o[], n;
  g = 1;
  n = 0;
  while(g > 0) {
    disp(p[]);
    if(p[0] == 1) {
      if(p[1] == 0) {
        p[0] = 0;
      }
    }
    if(p[0] > 0) {
      if(p[1] > 0) {
        p[1] = p[1] - 1;
        o[n++] = 1;
        print "L\n";
      } else {
        for(i = 1; i < p[0]; ++i) {
          p[i] = p[i + 1];
        }
        p[0] = p[0] - 1;
        o[n++] = 0;
        print "R\n";
      }
    } else {
      g = 0;
    }
  }
  r = 0;
  c = 0;
  for(i = n - 1; i >= 0; --i) {
    if(o[i] > 0) {
      r++;
      c = c * 2;
      print "op1\n";
    } else {
      r++;
      c = c * 2 + 1;
      print "op0\n";
    }
  }
  rc[0] = r;
  rc[1] = c;
  print "(",r,",",c,")\n";
}

define void bpseq(n,*p[]) {
  auto s, r, c, w;
  s = scale;
  scale = 0;
  w = 1;
  r = 0;
  c = 0;
  if(n == 0) {
    p[0] = 0;
    return;
  }
  while(n >= w) {
    w = w * 2;
    ++r;
  }
  print "2^",w,">n@r=",r,"\n";
  c = n - w / 2;
  bptree(r,c,p[]);
  scale = s;
}

define void nbpord(n,*s[]) {
  auto i, p[];
  for(i = 0; i < n; ++i) {
    bpseq(i,p[]);
    print "seq#",i,":";
    disp(p[]);
    s[i + 1] = bp2num(p[]);
  }
  s[0] = n;
}

define void bpordnth(n,*s[]) {
  auto p[], i, rc[], w, j;
  for(i = 1; i <= n; ++i) {
     basep(i,p[]);
     inbptree(p[],rc[]);
     if(rc[0] > 0) {
       w = rc[0] - 1;
       print "2^",w,"+",rc[1],"=";
       s[i] = 2^w + rc[1];
     } else {
       s[i] = 0;
     }
     print s[i],"\n";
  }
  s[0] = n;
}

---snip---

Some new developments, too... run-length encoding, and converting a base-prime number to a binary string (which can in turn be evaluated as a normal number, for further pattern-finding)

BP2RLE:
<< 0 0 0 -> DAT LST CNT RLE
  << DAT DUP
    IF SIZE 0 =/=
    THEN 1
      << DUP
        IF LST ==
        THEN 1 CNT + 'CNT' STO DROP
        ELSE
          IF CNT 0 >
          THEN LST CNT 2 ->ARRY 1 RLE + 'RLE' STO 0 'CNT' STO SWAP
          END 'LST' STO 1 'CNT' STO
        END
      >> DOSUBS
      IF CNT 0 >
      THEN
        IF RLE 0 =/=
        THEN OBJ-> DROP
        END LST CNT 2 ->ARRY 1 RLE + 'RLE' STO RLE ->LIST
      END
    END
  >>
>>

BP2BINSTR:
<< IF DUP SIZE 0 == THEN DROP 0
ELSE 1 << "0" SWAP IF DUP 0 >
THEN 1 SWAP START "1" + NEXT
ELSE DROP END >> DOSUBS REVLIST
OBJ-> DUP 1 + ROLL TAIL 2 PICK
1 + ROLLD ->LIST << + >> STREAM
"#" SWAP + "b" + OBJ-> B->R END
>>

RLE2BP:
<< LIST-> IF DUP 0 =/= THEN { } ->
B << 1 SWAP FOR X OBJ-> DROP DUP
1 SWAP FOR Y 2 PICK Y 2 +
ROLLD NEXT SWAP DROP ->LIST B
SWAP + 'B' STO NEXT B >> ELSE {
} END >>

BINSTR2BP:
<< 0 R->B ->STR SWAP BIN R->B ->STR
DUP SIZE DUP 3 ROLLD 1 - 3 SWAP
SUB "0" + SWAP 0 { } -> S L
  << 3 - 1 SWAP
    FOR X DUP X X SUB "0"
      IF =/=
      THEN S 1 + 'S' STO
      ELSE S 1 ->LIST L + 'L' STO 0 'S' STO
      END
    NEXT DROP S 1 ->LIST L +
  >> SWAP DUP SIZE DUP SUB
  IF DUP "h" ==
  THEN HEX
  END
  IF DUP "o" ==
  THEN OCT
  END
  IF DUP "d" ==
  THEN DEC
  END DROP
>>

And two functions for generating the increments along a sequence (the second program is only the increments, whereas the first program includes the first element at the beginning of the output sequence)...
DELTAS0:
<< DUP 0 SWAP LIST-> DROP ->LIST - >>

DELTAS1:
<< DELTAS0 TAIL >>

Don't forget that MOD, -, *, +, GCD, and LCM are all interesting routines to pass two lists of equal length into (and the first four of those can be a list and a scalar)

Tags: baseprime, math, primes
Subscribe
  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 2 comments