通过计算除了指数只 [英] Calculate exponents via addition only

查看:145
本文介绍了通过计算除了指数只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在编写一个非常简单的程序,在我们构建了一类的处理器执行。它不必乘或除的能力。我们然而,有支持加,减,与,或,和分支的回路控制(如分公司在平等的,如果你熟悉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.

这篇关于通过计算除了指数只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆