Oliver and others:
OK, I looked at my Period code and realized why it wasn't complete. Period attempts to find the period of a fraction by figuring out where the fraction starts repeating, using the modsto function: For the number n, 1/n repeats with a period length of r in this situation: For integer n, find the smallest integer r where 10^r = 1 mod n.
Now, this can lead to three results: either you will reach a situation where modsto gives you 0, in which case the invese of n terminates (e.g. 1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100), or modsto will reach 1, and it will return the period of the inverse (e.g. 3 (1), 7 (6), 9 (1), 11 (2) and so on), or it will loop, reaching neither 0 nor 1 (for example 6, which loops on the number 4).
Here is the original program:
≪ =:n @dup 1 - 1 @swap FOR x 10 x n modpow @dup IF 0 == THEN @drop "=terminates" @tag BREAK END IF 1 == THEN x "=period" @tag BREAK END NEXT ≫
I decided I wanted to handle the third case, so I added a little to my program, but it doesn't work:
≪  =a 0 SF =:n @dup 1 - 1 @swap FOR x 10 x n modpow @dupdup a + =a IF 0 == THEN @drop "=terminates" @tag 0 CF BREAK END IF 1 == THEN x "=period" @tag 0 CF BREAK END NEXT IF 0 FS? THEN a size =:z 1 @swap 1 - FOR x x 1 + z FOR y IF a[x] a[y] == THEN y x - "=period" @tag 0 CF BREAK END NEXT IF 0 FS? THEN NEXT ELSE BREAK END≫
I have attempted to highlight the added code, but essentially I am doing this: An array variable (thanks Oliver!) called a will hold the results of the modpow function, and flag 0 will be set while the answer has NOT been found, and cleared when it has been found (all rational fractions either terminate or repeat with a period).
So, the first thing I do is set the array up and set flag 0.
Next, I store the number in n and leave it in the stack, subtract 1 from it and start a loop from 1 to that number. The loop index is x.
In the loop, I try to find the answers with the modpow function, and duplicate them twice in the stack (I changed @dup to @dupdup) so I can add the result to the array (a + =a) as well as checking whether it equals 0 or 1.
If it equals 0, I drop the result of modpow and append the tag "=terminates" to the number I started with, and clear the flag before I break out of the loop.
If it equals 1, I append the tag "=period" to the result of modpow and clear the flag before I break out of the loop.
Otherwise, I continue to the end of the loop and set up to look for the "repeats" option.
I figure out the length of the array (size) and store that in z, then start two loops (counters x and y), the first looks at the items from the array from 1 to z-1 and the second from x+1 to z. If the same number is found in positions x and y, I compute y-x as the period, tag it with "=period", clear the flag and break out of the inner loop. At the end of the inner loop, I check to see if the flag is still set; if so, I end the outer loop, otherwise I break out.
Here is what happens when I attempt this: A message comes up that says:
Program execution error
Detail: Result of expression "I.apply"
[undefined] is not a function.
I can't make out if it is the letter capital i or the letter lowercase L in the message.
In the stack are the original number (level 2) and the number 0 (level 1).
I believe the problem comes about in the first loop, where I am attempting to append the result of modpow to the variable a. That might only be my first problem, though.
Any ideas on this? And, can you point me to where I could figure out error messages?