(50g) Integer Ratio to Exact Repeating Decimal
|
01-20-2018, 05:54 AM
(This post was last modified: 01-20-2018 03:20 PM by Joe Horn.)
Post: #1
|
|||
|
|||
(50g) Integer Ratio to Exact Repeating Decimal
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-991...l#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 <0|ΙΈ|0> -Joe- |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
(50g) Integer Ratio to Exact Repeating Decimal - Joe Horn - 01-20-2018 05:54 AM
RE: (50g) Integer Ratio to Exact Repeating Decimal - Gerald H - 01-20-2018, 05:13 PM
RE: (50g) Integer Ratio to Exact Repeating Decimal - Joe Horn - 01-20-2018, 09:18 PM
RE: (50g) Integer Ratio to Exact Repeating Decimal - Gerald H - 01-20-2018, 09:27 PM
RE: (50g) Integer Ratio to Exact Repeating Decimal - Joe Horn - 01-20-2018, 11:05 PM
|
User(s) browsing this thread: 1 Guest(s)