(50g) Integer Ratio to Exact Repeating Decimal - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (50g) Integer Ratio to Exact Repeating Decimal (/thread-9973.html) |
(50g) Integer Ratio to Exact Repeating Decimal - Joe Horn - 01-20-2018 05:54 AM This 50g program expresses any ratio of two integers as an exact decimal number, indicating which digits repeat and which digits do not repeat. Examples: Input: 5/12 Output: "0.41_6_" which means 0.416666666... with the 6 repeating forever. The underscores "_" indicate the repeating digit(s). Input: 13/18 Output: "0.7_2_" which means 0.72222222... Input: 71/17 Output: "4._1764705882352941_" (all 16 digits repeat forever) Input: 22/7 Output: "3._142857_" Input: 15/8 Output: "1.875" (the lack of underscores indicates that the decimal terminates, with no digits repeating forever) N.B. The longer the repeating section, the longer it takes for the program to run. Example: 115/226 takes 13 seconds to return the answer, which includes a repeating section of 112 digits. Code: %%HP: T(3)A(R)F(.); BYTES: 324.5 #3740h EDIT: A step-by-step analysis of the algorithms used above can be found here: http://www.hpmuseum.org/forum/thread-9919-post-88913.html#pid88913 and an exploration of alternatives for the first 9 lines of code (between FXND and MAX) can be found here: http://www.hpmuseum.org/forum/thread-9955.html EDIT 2: The variables used in the code above are: n: Numerator of input d: Denominator of input f: length of Fixed (non-repeating) section of digits r: the saved Remainder RE: (50g) Integer Ratio to Exact Repeating Decimal - Gerald H - 01-20-2018 05:13 PM Edited to remove DUP1PUTLAM_: A very nice piece of programming, congratulations. I've taken the liberty of writing a Sys version, in fact two. The first uses the function ^Factors to find number of 2's & 5's, the second uses my Sys programme from this thread http://www.hpmuseum.org/forum/thread-9955.html which for all fractions I've tested proves faster. Trust the programmes are of some interest. Size: 305.5 CkSum: # C2A3h Code: :: Size: 276.5 CkSum: # E5E6h Code: :: RE: (50g) Integer Ratio to Exact Repeating Decimal - Joe Horn - 01-20-2018 09:18 PM "DUP1PUTLAM_" won't compile on a standard HP 50g. Can it be replaced with "DUP 1PUTLAM"? RE: (50g) Integer Ratio to Exact Repeating Decimal - Gerald H - 01-20-2018 09:27 PM Yes, DUP 1PUTLAM is the correct reading. Sorry, I intended to remove all ?_s. (Now done.) RE: (50g) Integer Ratio to Exact Repeating Decimal - Joe Horn - 01-20-2018 11:05 PM Thanks, Gerald! Your System RPL programs are much faster than my User RPL program, roughly 16 times faster when the repeating section is large! Good job! Input: 1/503 (repeating section is 502 digits long) My program: 60 seconds Your 1st program: 3.61 seconds! Your 2nd program: 3.56 seconds! |