[math-fun] decimal expansions of P/Q in radix R and their uses
Hardy & Wright "Intro to the theory of numbers" chapter 9 is on this topic. Assume gcd(P,Q)=1. thm 135: P/Q has terminating or eventually-periodic expansion. If gcd(Q,R)=1 then periodic, otherwise terminating. The period length is the order of R mod Q. So, the conjecture that 1/Q where Q is "the first prime greater than 10^n" will have period exceeding 10^n is undoubtably false eventually, in fact 10 should presumably be a square mod Q eventually about half the time so it should be false at least half the time. You can check that P/Q with Q prime will have maximum period in radix R by computing the order of R mod Q and verifying it is Q-1, which you need to do by computing R^((Q-1)/d) mod Q for each divisor d of Q-1, and verifying none of these are 1. The idea that we can identify numbers like 1.004009016025036... by use of "generating functions" is silly in comparison with the much better and simpler idea that you can find the optimum rational approximations to ANY decimal X by use of (Euclid's) continued fraction expansion of X: 1.004009016025036049064081100 = [1, 249, 2, 3, 1, 1, 14, 7, 4, 2, 4, 7, 7, 1, large] then 1 + 1/(249+1/(2+1/(3+1/(1+1/(1+1/(14+1/(7+1/(4+1/(2+1/(4+1/(7+1/(7+1)))))))))))); = 1001000000 / 997002999 = 1.004009016025036049064081100121144169196225256289324361... Marsaglia suggested an actual use for all this, which is that the radix R expansion of some rational P/Q with huge period could provide a good and efficient pseudorandom number generator. It is possible by clever choice of P,Q,R to obtain huge periods (e.g. of order 2^500) but with no explicit use of multiprecision arithmetic. These go under the names "Marsaglia/Zaman add-with-carry, subtract-with-borrow, and multiply-with-carry" generators and they are implemented with extremely simple recurrences that effectively implement division of a huge multiprecision number by Q but because of the ultrasparse binary structure of the particular Q he chooses (and in some versions one can generate the quotient "backwards" too) this is easy. As the radix R, Marsaglia uses either 2^wordsize or 2^wordsize-1. As Q, Marsaglia uses a prime of the form Q=a*R^k-1 chosen so that R is a primitive root mod Q or anyhow having as large multiplicative order as possible. As P, pretty much any gunk filling an array of k machine words, will do. Actually, now that I am looking at this, http://en.wikipedia.org/wiki/Multiply-with-carry it occurs to me there ought to be a more general idea: Instead let Q be a prime of form a*R^k-b for integers a,b that fit well within one machine word, such that R has multiplicative order as large as possible mod Q. I think the recurrence-system instead will then be x[n] = (a * x[n-k] + c[n-1]) mod R c[n] = floor( (a*x[n-k] + b*c[n-1]) / R ) for n=k,k+1,k+2,... When implementing, x of course does not need to be an infinite-length array, but only with k entries since you can "wrap" it mod k, and c does not need to be an array at all, only a single machine word. As the math-funners have pointed out, though, certain Q lead to rather ridiculously obviously-nonrandom sequences (expansions of 1/Q mod R), albeit that is disguised to a considerable degree by the fact we are computing P/Q not 1/Q and P is made of truly-random "seed" bits. Any such generator is going to fail a "continued fraction test" of randomness almost instantly. This objection is defeated by: I also once invented a way to do the usual linear congruential generator x <-- (A*x+B) mod M to get huge periods without explicit use of multiprecision arithmetic, but it is considerably more complicated than Marsaglia's idea, although also quite efficient and probably "more random." My idea was to carefully choose M to be a prime of the form 2^a+-2^b+-1 where a and b are multiples of the computer wordsize; A is chosen to be a "sparse" multiprecision (a words long) number which happens to be a primitive root mod M. The sparsity structure of A is carefully selected to try to get both computational efficiency and fast graph-theoretical "expansion."
On Sun, Jan 29, 2012 at 9:19 AM, Warren Smith <warren.wds@gmail.com> wrote:
The idea that we can identify numbers like 1.004009016025036... by use of "generating functions" is silly in comparison with the much better and simpler idea that you can find the optimum rational approximations to ANY decimal X by use of (Euclid's) continued fraction expansion of X: 1.004009016025036049064081100 = [1, 249, 2, 3, 1, 1, 14, 7, 4, 2, 4, 7, 7, 1, large] then 1 + 1/(249+1/(2+1/(3+1/(1+1/(1+1/(14+1/(7+1/(4+1/(2+1/(4+1/(7+1/(7+1)))))))))))); = 1001000000 / 997002999 = 1.004009016025036049064081100121144169196225256289324361...
But without the "silly" generating functions, do we have any explanation of why if we take the continued fraction expansion of 1.004009016025036049064081100, and truncate before the first large entry, we end up with a decimal that continues with 121144169196225256289324361... Surely this is not coincidence! Continued fractions give us an efficient way to find the smallest-denominatored fraction that starts with the first ten three-digit squares, but unless I'm missing something, they give no insight as to why this expansion should continue with the next 10 3-digit squares, and the generating function approach does. Andy Latto
Suppose you are looking for a rational that yields a string of squares: v = .001004009016025036... If you start with long enough initial snippet of v and apply Euclid's algorithm, you will undoubtedly at some point arrive at 1001000/997002999 However, Euclid will not tell you outright how big your initial snippet must be to yield "the answer" as a convergent, or which convergent is "the answer". Furthermore, it is a bit of computational work to arrive at "the answer". The "generating function" approach is really a series evaluation approach, where we evaluate SUM(n = 1..inf; p(n)/(b^d)^n) with polynomial p(n) or order k, base b and d digits per block, as a rational function of n. The results tells you that the denominator must divide (b^d-1)^(k+1). In the original example, the base is 10, the blocks are 3 digits long, the block values are given by polynomial f(n) = n^2, which is order k = 2, so we use denominator (10^3-1)^(2+1) = 997002999, from which it is straightforward to obtain and simplify the rational given above. The polynomial function can be generalized to a linear recurrence while still evaluating to a rational. The denominator in this case will be related to block value recurrence. For example, with v = .001002004008016032... (powers of 2) The recurrence for the block value recurrence is f(n+1) - 2f(n). The finite linear recurrence SUM(a_i f(n+i)) = 0 has the associated polynomial p(x) = SUM(a_i x^i) = 0, so our associate polynomial is p(x) = x - 2. Our denominator will then be p(b^d), in this case p(10^3) = p(1000) = 1000 - 2 = 998, and indeed v = 1/998. Or take v = .001001002003005008... (Fibonacci numbers) In this case, the block value recurrence if f(n+2) - f(n+1) - f(n) = 0, yielding polynomial p(x) = x^2 - x - 1, so that p(b^d) = p(1000) = 1000^2 - 1000 - 1 = 998999, and again v = 1000/998999. In the case where f is a polynomial of order k, we get associated polynomial p(x) = (x-1)^(k+1), yielding denominator (b^d-1)^(k+1) as given above. Also, the series approach tells you something that Euclid does not, specifically, that your blocks will eventually carry and bleed over into previous blocks and spoil your nice pattern. On 1/29/2012 9:19 AM, Warren Smith wrote:
The idea that we can identify numbers like 1.004009016025036... by use of "generating functions" is silly in comparison with the much better and simpler idea that you can find the optimum rational approximations to ANY decimal X by use of (Euclid's) continued fraction expansion of X: 1.004009016025036049064081100 = [1, 249, 2, 3, 1, 1, 14, 7, 4, 2, 4, 7, 7, 1, large] then 1 + 1/(249+1/(2+1/(3+1/(1+1/(1+1/(14+1/(7+1/(4+1/(2+1/(4+1/(7+1/(7+1)))))))))))); = 1001000000 / 997002999 = 1.004009016025036049064081100121144169196225256289324361...
The continued-fraction approximations method ("Euclid's" [1]) seems appealing [2], but it is no guarantee. Suppose I use continued fractions to find A/B=0.001004009016025036... How do I know that the fraction continues as I want it to? I could look at 100 digits, but what if it fails just beyond the last digit I checked? - Robert [1] See http://en.wikipedia.org/wiki/Euclidean_algorithm#Continued_fractions [2] An example, in the Unix tool bc. The answer is probably the one just before the big jump in size: cfrac(0.010409162536496481) 1 / 0 0 / 1 1 / 96 14 / 1345 29 / 2786 130 / 12489 289 / 27764 708 / 68017 997 / 95781 2702 / 259579 3699 / 355360 6401 / 614939 10100 / 970299 10598007101 / 1018142147732 74186059807 / 7126996004423 The source code for "cfrac" is: define cfraccore(x,m) { auto xi,n,d,n1,d1,n2,d2,s; if (x<0) { x = -x } if (x==0) { return(0) } s=10^scale n=1 d=0 n1=0 d1=1 i=0 while((n+d)*(n+d)<s) { if (m==0) { print n, " / ", d, "\n" } xi=floor(x) x=x-xi x=1/x n2=n1 n1=n d2=d1 d1=d n=n1*xi+n2 d=d1*xi+d2 if ((m==1) && (n+d<s)) { if (i>1) { print ", "; } else if (i>0) { print "; "; } print xi; } i=i+1 } if (m==1) { print "\n" } return(1) } print " cfrac(x) prints successive continued-fraction approximations to x" print "\n" define cfrac(x) { auto ignore ignore=cfraccore(x,0) } print " cfseq(x) prints terms of simple continued fraction for x" print "\n" define cfseq(x) { auto ignore ignore=cfraccore(x,1) } On Sun, Jan 29, 2012 at 09:19, Warren Smith <warren.wds@gmail.com> wrote:
[...]
The idea that we can identify numbers like 1.004009016025036... by use
of "generating functions" is silly in comparison with the much better and simpler idea that you can find the optimum rational approximations to ANY decimal X by use of (Euclid's) continued fraction expansion of X:
1.004009016025036049064081100 = [1, 249, 2, 3, 1, 1, 14, 7, 4, 2, 4,
7, 7, 1, large] then 1 + 1/(249+1/(2+1/(3+1/(1+1/(1+1/(14+1/(7+1/(4+1/(2+1/(4+1/(7+1/(7+1)))))))))))); = 1001000000 / 997002999 = 1.004009016025036049064081100121144169196225256289324361...
[...]
-- Robert Munafo -- mrob.com Follow me at: gplus.to/mrob - fb.com/mrob27 - twitter.com/mrob_27 - mrob27.wordpress.com - youtube.com/user/mrob143 - rilybot.blogspot.com
participants (4)
-
Andy Latto -
David Wilson -
Robert Munafo -
Warren Smith