HP Forums
(49G) Exact Division to Integer Base - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (49G) Exact Division to Integer Base (/thread-4008.html)



(49G) Exact Division to Integer Base - Gerald H - 05-28-2015 09:56 AM

If you wish to calculate G/H in base B, G, H, B positive integers this is the programme.

eg For input

107
103
4567

the programme calculates 107/103 base 4567 & returns

:4567: { 1 "." "°" 177 1640 2616 221 3192 2128 1418 3990 2660 1773 2704 3325 2216 4522 3015 487 3369 3768 4034 4212 1285 3901 4123 2749 310 1729 1152 3813 1019 3724 2483 133 88 3103 3591 2394 1596 1064 709 1995 1330 886 3635 3946 1108 2261 1507 2527 1684 4167 4300 4389 2926 1950 4345 1374 2438 3148 576 1906 2793 1862 1241 2350 44 1551 4079 1197 798 532 354 3281 665 443 1817 4256 2837 3414 753 3547 842 2083 4433 4478 1463 975 2172 2970 3502 3857 2571 3236 3680 931 620 3458 2305 3059 2039 2882 399 266 }

where everything after the "°" recurs.

Programme name: XdYB

Code:
::
  CK3&Dispatch
  # AAA
  ::
    FPTR2 ^CK3Z
    BINT3
    NDUP
    FPTR2 ^QMul
    FPTR2 ^QMul
    FPTR2 ^QIsZero?
    case2drop
    ::
      FPTR2 ^DupQIsZero?
      ?SEMI
      SETIVLERR
    ;
    ZINT 0
    FPTR2 ^2LAMBIND
    2DUP
    FPTR2 ^ZDIVext
    SWAP
    2GETLAM
    NULL{}
    3UNROLL
    ::
      BEGIN
      OVER
      FPTR2 ^QIsZero?
      case
      AGAIN
      DUPUNROT
      FPTR2 ^ZDIVext
      4ROLLSWAP
      >HCOMP
      SWAPROT
      AGAIN
    ;
    RDROP
    2DROP
    "."
    >TCOMP
    4UNROLL
    FPTR2 ^QIsZero?
    ::
      case2DROP
      NULL{}
      DUP4UNROLL
      3UNROLL
      BEGIN
      DUPUNROT
      FPTR2 ^ZDIVext
      5ROLL
      ROT
      >TCOMP
      4UNROLL
      2GETLAM
      FPTR2 ^QMul
      SWAPROT
      1GETLAM
      >TCOMP
      UNROTOVER
      1PUTLAM
      OVER
      4PICK
      FPTR2 ^ListPos
      DUP#0=
      WHILE
      DROP
      REPEAT
      4UNROLL3DROP
      1GETLAM
      FPTR2 ^QIsZero?
      ::
        caseDROP
        "°"
        SWAP
        FPTR2 ^INSERT{}N
      ;
      CDRCOMP
      &COMP
    ;
    2GETLAM
    ABND
    DO>STR
    >TAG
  ;
;



RE: (49G) Exact Division to Integer Base - Gerald H - 01-20-2018 11:36 AM

A slightly smaller & faster version of the programme:

Size: 288.

CkSum: # 3E5Fh

Code:
::
  CK3&Dispatch
  # AAA
  ::
    FPTR2 ^CK3Z
    BINT3
    NDUP
    FPTR2 ^QMul
    FPTR2 ^QMul
    ZINT 0
    EQUAL
    case2drop
    ::
      FPTR2 ^DupQIsZero?
      ?SEMI
      SETIVLERR
    ;
    ZINT 0
    FPTR2 ^2LAMBIND
    2DUP
    FPTR2 ^ZDIVext
    SWAP
    2GETLAM
    NULL{}
    3UNROLL
    ::
      BEGIN
      OVER
      ZINT 0
      EQUALcase
      AGAIN
      DUPUNROT
      FPTR2 ^ZDIVext
      4ROLLSWAP
      >HCOMP
      SWAPROT
      AGAIN
    ;
    RDROP
    2DROP
    "."
    >TCOMP
    4UNROLL
    ZINT 0
    EQUAL
    ::
      case2DROP
      NULL{}
      DUP
      2SWAP
      BEGIN
      DUPUNROT
      FPTR2 ^ZDIVext
      5ROLL
      ROT
      >TCOMP
      4UNROLL
      2GETLAM
      FPTR2 ^QMul
      SWAPROT
      1GETLAM
      >TCOMP
      UNROTOVER
      1PUTLAM
      OVER
      4PICK
      FPTR2 ^ListPos
      DUP#0=
      WHILE
      DROP
      REPEAT
      4UNROLL3DROP
      1GETLAM
      ZINT 0
      EQUAL
      ::
        caseDROP
        "°"
        SWAP
        FPTR2 ^INSERT{}N
      ;
      CDRCOMP
      &COMP
    ;
    2GETLAM
    ABND
    DO>STR
    >TAG
  ;
;



RE: (49G) Exact Division to Integer Base - Gerald H - 01-20-2018 11:46 AM

An example:

For input

9384846311045874254
777700
98765432167

The programme returns

:98765432167: { 122 18054990742 "." "°" 20453346936 12295578772 71144384328 83919757113 4804797943 42566542397 91923605122 42104781935 84073677267 7421440561 87049466911 58009864518 58163784672 60780427291 6497919637 71349611200 87408613937 64115363961 63191843037 47491987326 76890736745 82842316034 85253731781 27482367303 33023492848 28457194945 49595562765 13886087030 98183024719 49749482919 16502729649 43900517066 15835742315 32561732386 20607267090 14912221391 16861876675 50006016509 20863800680 19273292421 91000084198 26404926225 14706994519 13373019850 89460882658 238500040 63704910217 56214129388 27636287457 35640135466 72940119459 15681822161 29945089767 74889774743 48825961995 802873938 73299266485 21787321604 34973148132 61601334779 }

in

TIME: 1.3066s on a 50g.


RE: (49G) Exact Division to Integer Base - Gerald H - 01-20-2018 11:56 AM

Another example:

Input

488669
800
2

returns

:2: { 1 0 0 1 1 0 0 0 1 0 "." 1 1 0 1 0 "°" 1 1 0 0 0 0 1 0 1 0 0 0 1 1 1 1 0 1 0 1 }