计算机编程艺术练习题:第 1 章,问题 8

发布于 2024-07-14 04:31:11 字数 1008 浏览 12 评论 0原文

我正在做 TAOCP 第 1 卷第 3 版的练习,但无法理解以下练习的答案中使用的语法。

第一章练习8

计算正整数m & 的最大公约数 n 通过指定 Tj,sj,aj,bj

让你的输入由字符串表示ambn(m a 后面跟着 n 个 b)

答案:

设 A = {a,b,c},N=5。 该算法将以字符串 agcd(m,n) 终止。

    j     Tj     sj    bj    aj
    0     ab  (empty)  1    2   Remove one a and one b, or go to 2.
    1   (empty)  c     0    0   Add c at extreme left, go back to 0.
    2     a      b     2    3   Change all a's to b's
    3     c      a     3    4   Change all c's to a's
    4     b      b     0    5   if b's remain, repeat

我无法理解的部分就是如何解释该表。 另外,当 Knuth 说这将以字符串 agcd(m,n) 终止时 - 为什么 gcd(m,n) 的上标?

谢谢你的帮助!

编辑了更多问题:

什么是 Tj - 请注意 T = Theta

什么是 sj - 请注意 s = phi

如何解释列 b j 和 aj

为什么 Knuth 将解决方案中的新符号转换为他在文本中没有解释的示例? 只是令人沮丧。 谢谢!!!

I'm doing the exercises to TAOCP Volume 1 Edition 3 and have trouble understanding the syntax used in the answer to the following exercise.

Chapter 1 Exercise 8

Computing the greatest common divisor of positive integers m & n by specifying Tj,sj,aj,bj

Let your input be represented by the string ambn (m a's followed by n b's)

Answer:

Let A = {a,b,c}, N=5. The algorithm will terminate with the string agcd(m,n)

    j     Tj     sj    bj    aj
    0     ab  (empty)  1    2   Remove one a and one b, or go to 2.
    1   (empty)  c     0    0   Add c at extreme left, go back to 0.
    2     a      b     2    3   Change all a's to b's
    3     c      a     3    4   Change all c's to a's
    4     b      b     0    5   if b's remain, repeat

The part that I have trouble understanding is simply how to interpret this table.
Also, when Knuth says this will terminate with the string agcd(m,n) -- why the superscript for gcd(m,n) ?

Thanks for any help!

Edited with more questions:

What is Tj -- note that T = Theta

What is sj -- note that s = phi

How do you interpret columns bj and aj?

Why does Knuth switch a new notation in the solution to an example that he doesn't explain in the text? Just frustrating. Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-07-21 04:31:11

这是该练习答案的实现。 也许有帮助。

顺便说一句,该表似乎描述了马尔可夫算法

据我所知,到目前为止,您从第一个命令集 j = 0 开始。将所有出现的 Tj 替换为 sj 并跳转到下一个命令行取决于您是否替换了任何内容(在这种情况下跳转到 bj,如果没有替换任何内容,则跳转到 aj)。

编辑:新答案:

A = {a,b,c} 似乎是您可以操作的字符集。 c 在算法过程中出现(添加到左侧,后来再次被 a 替换)。

Theta 和 phi 可能是您通常用于“原始”和“替换”之类的希腊字符,尽管我不知道它们是什么。

bj 和 aj 是接下来要执行的表行。 这与最后一栏中的人类可读的描述相匹配。

我唯一无法回答的是为什么 Knuth 在没有任何解释的情况下使用这个符号。 我又浏览了一遍书中的前几章和解决方案,他没有在任何地方提到这一点。

EDIT2:gdc(2,2) = 2 的示例

    Input string: aabb
    Line 0: Remove one a and one b, or go to 2.
    => ab => go to 1
    Line 1: Add c at extreme left, go back to 0.
    => cab => go to 0
    Line 0: Remove one a and one b, or go to 2.
    => c => go to 1
    Line 1: Add c at extreme left, go back to 0.
    => cc => go to 0
    Line 0: Remove one a and one b, or go to 2.
    No ab found, so go to 2
    Line 2: Change all a's to b's
    No a's found, so go to 3
    Line 3: Change all c's to a's
    => aa
    Line 4: if b's remain, repeat
    No b's found, so go to 5 (end).

    => Answer is "aa" => gdc(2,2) = 2

顺便说一句,我认为第 1 行的描述应该是“删除一个“ab”,或转到 2。” 这让事情变得更清楚了一些。

Here's an implementation of that exercise answer. Perhaps it helps.

By the way, the table seems to describe a Markov algorithm.

As far as I understand so far, you start with the first command set, j = 0. Replace any occurencies of Tj with sj and jump to the next command line depending on if you replaced anything (in that case jump to bj, if nothing has been replaced, jump to aj).

EDIT: New answers:

A = {a,b,c} seems to be the character set you can operate with. c comes in during the algorithm (added to the left and later replaced by a's again).

Theta and phi could be some greek character you usually use for something like "original" and "replacement", although I wouldn't know they are.

bj and aj are the table lines to be next executed. This matches with the human-readable descriptions in the last column.

The only thing I can't answer is why Knuth uses this notation without any explanations. I browsed the first chapters and the solutions in the book again and he doesn't mention it anywhere.

EDIT2: Example for gdc(2,2) = 2

    Input string: aabb
    Line 0: Remove one a and one b, or go to 2.
    => ab => go to 1
    Line 1: Add c at extreme left, go back to 0.
    => cab => go to 0
    Line 0: Remove one a and one b, or go to 2.
    => c => go to 1
    Line 1: Add c at extreme left, go back to 0.
    => cc => go to 0
    Line 0: Remove one a and one b, or go to 2.
    No ab found, so go to 2
    Line 2: Change all a's to b's
    No a's found, so go to 3
    Line 3: Change all c's to a's
    => aa
    Line 4: if b's remain, repeat
    No b's found, so go to 5 (end).

    => Answer is "aa" => gdc(2,2) = 2

By the way, I think description to line 1 should be "Remove one "ab", or go to 2." This makes things a bit clearer.

自此以后,行同陌路 2024-07-21 04:31:11

gcd(m,n) 的上标是由于该表中数字的表示方式决定的。

例如:m=> ^m
n => b^n

gcd(m,n) => a^gcd(m,n)

看起来像是正在实施欧几里得算法。
即,

gcd(m,n):
  if n==0:
    return m
  return gcd(n,m%n)

数字被表示为幂以便能够进行模运算m%n。

例如,4 % 3 将计算如下:
4 个'a' (a^4) mod 3'b's (b^3),将留下 1 个'a' (a^1)。

The superscript for gcd(m,n) is due to how numbers are being represented in this table.

For example: m => a^m
n => b^n

gcd(m,n) => a^gcd(m,n)

It looks to be like Euclids algorithm is being implemented.
i.e.

gcd(m,n):
  if n==0:
    return m
  return gcd(n,m%n)

The numbers are represented as powers so as to be able to do the modulo operation m%n.

For example, 4 % 3, will be computed as follows:
4 'a's (a^4) mod 3 'b's (b^3), which will leave 1 'a' (a^1).

只有一腔孤勇 2024-07-21 04:31:11

am 的概念可能是状态机上下文中输入字符串的概念。

这样的概念用于指代连续 am 个实例,即:

a4 = aaaa
b7 = bbbbbbb
a4b7a3 = aaaabbbbbbbaaa

而 agcd(m,n) 的意思是运行后(解决方案)状态机,结果字符串应该是 agcd(m,n) 实例,

换句话说,a 的数量结果中的 应该等于 gcd(m,n) 的结果

我同意 @schnaader 的观点,因为它可能是一个描述马尔可夫算法用法的表。

the notion of am is probably a notion of input string in the state machine context.

Such notion is used to refer to m instances of consecutive a, i.e.:

a4 = aaaa
b7 = bbbbbbb
a4b7a3 = aaaabbbbbbbaaa

And what agcd(m,n) means is that after running the (solution) state machine, the resulting string should be gcd(m,n) instances of a

In other words, the number of a's in the result should be equal to the result of gcd(m,n)

And I agree with @schnaader in that it's probably a table describing Markov algorithm usages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文