The Art of Computer Programming exercise question: Chapter 1, Question 8

后端 未结 3 2117
小蘑菇
小蘑菇 2021-02-08 20:07

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

3条回答
  •  礼貌的吻别
    2021-02-08 20:40

    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).

提交回复
热议问题