Periods of Reciprocals of Integers
|
12-30-2017, 02:19 PM
Post: #1
|
|||
|
|||
Periods of Reciprocals of Integers
I'm tutoring a friend's son in math, and recently we've been practicing the long-division algorithm.
We started looking at the reciprocals of integers, which led to a discussion of their decimal representations. Some of course "terminate" like 1/2 or 1/5. Others repeat forever, like 1/3 or 1/6. For simplicity we considered the "terminating" decimal representations to be repeating decimals, with '0' repeating forever. This made me wonder how to determine the period of the decimal representation of the reciprocal of an integer. I'm sure this has been answered long ago for the general case, but I like to see for myself. To this end I defined the "preamble" to be that part of the decimal that precedes the repeating part, and the "period" to be the length of one cycle of the repeating part. The division algorithm shows both that every such reciprocal 1/n must eventually repeat, and that the maximum possible period is (n-1). More to the point, the algorithm also reveals that the repetition of digits begins when a repeated remainder is first encountered. So... I coded up a program, first for my workhorse 32Sii, then for my 41CX. The challenge here is that the program must keep track of all the remainders it has seen, so that it can detect the first repetition. That means a bitmap, which means that the range is limited by available memory. That in turn means making the program as small as possible, while using as few registers as possible. I was able to split the program into six routines, each stored in extended memory. These six routines load one another as required, using GETP. I was able to squeeze the routines down to the point where the largest of the six requires 64 bytes. I just can't get it to 63 (which would improve range). The silver lining in that cloud is that it means I can use a more capable version of one of the other programs ("Q"), as long as that version is no larger than 70 bytes. Unlike the 32Sii which supports 36 bits per register, the 41CX supports only 30. By squeezing the programs down to 70 bytes or less, I can execute SIZE 309 which (since I use two registers for other things) gives the program a maximum range of 30*(309-2)=9210. The program actually runs through the division twice - first, to find the matching remainder, and second, to count off the preamble and period. I know it's slow, but I sacrificed time for space in order to maximize range. The user interface is as follows: If flag 00 is set, the program will display the decimal digits as it counts the preamble and period. Flag 01 is set by the program when it completes the count of the preamble and begins counting the period, and cleared when the entire calculation is complete. If flag 02 is set, the program will pause with TIME displayed, to allow a crude measure of elapsed time. Continue the program with R/S to see the actual results. Place "P" in Alpha XEQ GETP Once the initial program "P" is loaded, functionality is as follows: XEQ 01 = reset (reloads program "P") Place positive integer argument n (1 <= n <= 9210) in stack-X XEQ 02 = calculate preamble and period Returns: period-length in stack-X preamble-length in stack-Y n in stack-Z (also in Last-X) matched-remainder in stack-T If somehow we already know the matched-remainder, we can escape the 9210 limit as follows: Place matched-remainder in stack-Y Place n in stack-X XEQ 03 = calculate preamble and period Returns: the same stack of results as XEQ 02 above. The program may be interrupted (and continued) as desired to change flag 00 or 02 settings. However, the display of digits (SF 00) cannot be activated for the first time once the counting of the preamble has begun. The program may be terminated at any time, and reset by XEQ 01. If anyone could tell me how to squeeze program "R" down to 63 bytes without using an additional register or sacrificing functionality, I could extend the range to 9240... Code: 1 LBL "P" Code: 1 LBL "R" Code: 1 LBL "U" Code: 1 LBL "T" Code: 1 LBL "S" Code: 1 LBL "Q" |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Periods of Reciprocals of Integers - Macumazahn - 12-30-2017 02:19 PM
RE: Periods of Reciprocals of Integers - Thomas Klemm - 08-05-2018, 08:49 PM
RE: Periods of Reciprocals of Integers - Albert Chan - 08-06-2018, 12:59 AM
RE: Periods of Reciprocals of Integers - Thomas Klemm - 08-06-2018, 03:37 AM
RE: Periods of Reciprocals of Integers - Albert Chan - 08-06-2018, 02:05 PM
RE: Periods of Reciprocals of Integers - Thomas Klemm - 08-06-2018, 05:30 PM
RE: Periods of Reciprocals of Integers - Albert Chan - 08-06-2018, 07:41 PM
|
User(s) browsing this thread: 1 Guest(s)