通过计算除了指数只指数

由网友(不必怕)分享简介:我们正在编写一个非常简单的程序,在我们构建了一类的处理器执行。它不必乘或除的能力。我们然而,有支持加,减,与,或,和分支的回路控制(如分公司在平等的,如果你熟悉MIPS)。我们想一个整洁的计划,以在其上运行将是某种形式的X ^ n项目。当然,这些数字就必须要硬codeD,但由于我们的处理器的限制,是现实吗?We're...

我们正在编写一个非常简单的程序,在我们构建了一类的处理器执行。它不必乘或除的能力。我们然而,有支持加,减,与,或,和分支的回路控制(如分公司在平等的,如果你熟悉MIPS)。我们想一个整洁的计划,以在其上运行将是某种形式的X ^ n项目。当然,这些数字就必须要硬codeD,但由于我们的处理器的限制,是现实吗?

We're writing a very simple program to execute on a processor we've built for a class. It doesn't have the capability to multiply or divide. We do however, had support for addition, subtraction, and, or, and branching for loop control (like branch on equal if you are familiar with MIPS). We were thinking a neat program to run on it would be some sort of x^n program. Of course, those numbers would have to be hardcoded, but given the limitations of our processor, is it realistic?

有没有另外只计​​算指数? 谢谢你。

Is there an addition only calculation for exponents? Thanks.

推荐答案

有关小整数,为什么不呢?

For small integers, why not?

首先,使用重复加法实现倍增。然后,使用重复的乘法实现POW()。这将是缓慢的,但它会正常工作。

First, implement multiply using repeated addition. Then, implement pow() using repeated multiplication. It will be slow, but it will work fine.

目前的平方更快的算法幂,称为幂。然而,由于你没有一个快速的乘法,我不知道这是值得的 - 你可能想先工作实现快速乘法算法

There is a faster algorithm for exponentiation, called Exponentiation by Squaring. However, given that you don't have a fast multiply, I'm not sure it's worth it - you might want to first work on implementing a fast multiplication algorithm.

阅读全文

相关推荐

最新文章