The following warnings occurred:
Warning [2] count(): Parameter must be an array or an object that implements Countable - Line: 795 - File: showthread.php PHP 7.4.33 (FreeBSD)
File Line Function
/showthread.php 795 errorHandler->error





Post Reply 
(48G/49g/50g) Fractional Base Conversions
10-03-2022, 10:57 PM (This post was last modified: 10-03-2022 11:02 PM by John Keith.)
Post: #1
(48G/49g/50g) Fractional Base Conversions
Inspired by Gerald H's recent thread, these programs convert between integers and numbers in fractional base. The program I->FBL expects an integer n on level 3, the numerator p on level 2 and the denominator q on level 1. The program returns a list of integers representing the digits of n in base p/q, in standard order (units on the right, highest powers on the left). For example, if the level 3 argument is 15, level 2 is 3 and level 1 is 2, the result is { 2 1 0 1 0}, the digits of 15 in base 3/2. See A024629 for further information. First the program for the HP 49g and 50g, which should be used in exact mode.

Code:

\<< 1. \-> p q j
  \<<
    WHILE p IDIV2 SWAP         @ Quotient and remainder of p
      DUP 0 SAME NOT           @ While quotient not 0
    REPEAT q * 1. 'j' STO+     @ Multiply by q and increment counter
    END DROP j \->LIST REVLIST @ Drop extra 0 and make reversed list
  \>>
\>>

The following approximate version is HP48G compatible but limited to numbers < 10^12.

Code:

\<< 1 \-> p q j
  \<<
    WHILE p DUP2 / FLOOR ROT ROT MOD @ Equivalent of IDIV2
      SWAP DUP 0 SAME NOT
    REPEAT q * 1 'j' STO+
    END DROP j \->LIST REVLIST
  \>>
\>>

The next program, FBL->I converts a list as returned by the above program to an integer. The list should be on level 3, the numerator on level 2 and the denominator on level 1. For example, if the level 3 list is { 2 1 1 2 0 1 0 }, level 2 is 3 and level 1 is -2, the program returns 12, which is 2112010 in base -3/2. See A355904 and the linked paper by Knuth. Note that for negative base, the numerator must be positive and the denominator must be negative. If the signs are reversed, the result will be incorrect.

Code:

\<< DUP2 5 PICK SIZE \-> p q r s n @ r and s store powers of p and q
  \<< EVAL 2 n                     @ Explode list onto stack and begin
    START SWAP r * s / + EVAL      @ Multiply current number by r,
                                   @ divide by s and accumulate
      'r' p STO* 's' q STO*        @ Store next powers of p and q
    NEXT
  \>>
\>>

For approximate mode (or the 48G), the EVAL at the end of line 3 can be eliminated.

For users of the 49g or 50g with ListExt, here is a shorter and faster version:

Code:

\<< PICK3 SIZE \-> p q n
  \<< REV 1 p n LMSEQ * @ Reverse and multiply by powers of p
      1 q n LMSEQ /     @ Divide by powers of q
      LSUM EVAL         @ Sum and simplify
  \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
(48G/49g/50g) Fractional Base Conversions - John Keith - 10-03-2022 10:57 PM



User(s) browsing this thread: 1 Guest(s)