Bert_emme <
bertan...@gmail.com> writes:
> I'm wrote:
>
>> Give the "effective" formal algorithm for computing the greatest
>> common divisor of positive integer m and n. Let the input be
>> represente by the string (a^m)(b^n), that is, m a's follow n b's.
>> Take from "art of computer programming"
>> Donald E. Knuth
>
> To avoid ambiguity of terminology I rename the string (a^m)(b^n) as
> (q^m)(z^n).
>
> My solution is:
>
> Let A a set of finite letter {q,z} and A* the set of possible
> combination
> of element of set A with the structure (q^m)(z^n), with m,n >0.
Did Knuth make this restriction? I'd have thought that m,n >= 0 was
more natural.
> Now let N be a nonnegative integer and let Q be the set of all ($,,j)
> where $ is in A* and j is an integer 0<=j<=N.
> Let IN be the subset of Q with j = 0 and OUT be the subset with j = N.
> If mj and $ are string in A*, we say that mj occur in $ if $ has the
> form
> (p mj s) for string p,s where p is the shortest string for which
> (p mj s) = $.
> Now let rj the string that replace mj when it occurs in $.
> Let f a function that compute the gcd as:
>
> f(($,N)) = ($,N)
> f(($,j)) = ($,aj) se mj doesn't occur in $
> f(($,j)) = (p rj s, bj) se mj occur in $ where:
> { n = |m-n| e bj = min(m,n)
> { { (q^n)(z^bj) if n>=bj
> {{ and rj = {{
> { { (q^bj)(z^n) otherwise
> {
>
> I think that this is the solution of the exercise.
No, this is not yet a solution.
The main problem is that it is not finite. The N that bounds the size
of all the sets must be specified and is independent of the input. In
your example, the range of j depends on the input -- bj is min(m, n) and
there is no upper limit on how large that can be.
Once you have a solution you can say what N is. Lets say your solution
requires N to be 8 (my solution has N=8). You can then simply list the
8 numbers a0, a1, ... a9, the 8 numbers b0, b1, ... b8 and similarly for
the 8 match strings, mj and the 8 replacement strings rj.
> If someone want to comment it I appreciate him.
> with thanks of Ben that sure correct my error... I hope
Here's an example. Consider multiplication: give an effective
algorithm to compute the string (c^(nm)) from an input of the form
(a^n)(n^n). I.e. we want to turn "aaabb" into "cccccc" and "aabb" into
"cccc". The inputs "bbb" and "aa" both turn into "" since x*0 = 0.
In my solution (probably not optimal) N = 7. The "match" strings are:
"ab", "ab", "b", "x", "b", "a" and "b", the "replacement" strings are
"b", "xc", "xc", "b", "c", "" and "". The numbers aj, for 0 <= j < N
are 5, 4, 3, 1, 7, 6 and 7. The bj are 1, 2, 2, 3, 4, 5 and 6.
It's very hard to understand the solution when it's written like this,
but it specifies a function, p, of this form:
p(($, 0)) = ($/ab/b/, 1) or ($, 5)
p(($, 1)) = ($/ab/xc/, 2) or ($, 4)
p(($, 2)) = ($/b/xc/, 2) or ($, 3)
p(($, 3)) = ($/x/b/, 3) or ($, 1)
p(($, 4)) = ($/b/c/, 4) or ($, 7)
p(($, 5)) = ($/a//, 5) or ($, 6)
p(($, 6)) = ($/b//, 6) or ($, 7)
p(($, 7)) = ($, 7)
where $/m/r/ is the result of replacing the first occurrence of m in $
with r. If m does not occur in $, p takes the second value shown.
Notice how everything is finite.
The computation of p(("aabb", 0)) proceeds as follows:
p(("aabb",0)) = ("abb",1)
p(("abb",1)) = ("xcb",2)
p(("xcb",2)) = ("xcxc",2)
p(("xcxc",2)) = ("xcxc",3)
p(("xcxc",3)) = ("bcxc",3)
p(("bcxc",3)) = ("bcbc",3)
p(("bcbc",3)) = ("bcbc",1)
p(("bcbc",1)) = ("bcbc",4)
p(("bcbc",4)) = ("ccbc",4)
p(("ccbc",4)) = ("cccc",4)
p(("cccc",4)) = ("cccc",7)
Does this help?
--
Ben.