在Python中实现指数 [英] Implementing Exponentials in Python

查看:232
本文介绍了在Python中实现指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写一个程序,使用重复乘法计算a ^ b(a ** b)的值。不使用运算符(**)



我只知道如何使用**。

How to write a program that computes the value of a^b(a**b) using repeated multiplication. Without using the operator(**)

I only know how to do it with the **.

推荐答案

此处: http://docs.python.org/2/library/math.html [ ^ ]。



它不使用任何运营商,只是通话。正式,我回答。



但是,你的问题的含义并不清楚。你的意思是你想用其他函数作为乘法来实现指数吗?对于整数指数不?请注意,重复乘法只能是一个整数度。



是作业吗?这是一个整数幂,我只是给你一个想法:你应该避免做N次乘法。例如,要计算 N ** 5 ,您需要计算 N * N ,然后将结果乘以自身,然后将结果乘以 N 。这只是提示。对于数值数据,根本没有意义,但是,例如,如果操作数是矩阵,该怎么办?如果 N 很大,则乘以 N 次数不是一个选项,那么基于我的提示的算法是通往去。



-SA
Here: http://docs.python.org/2/library/math.html[^].

It does not use any operators, just the call. Formally, I answered.

However, the meaning of your question is not clear. Do you mean that you want to implement exponent using other functions as multiplication? For integer exponents of not? Note that "repeated multiplication" is only possible of this is an integer degree.

Is is a homework? Is this is an integer power, I'll just give you the idea: you should avoid doing N multiplications. For example, to calculate N**5, you need to calculate N*N, then multiply the result by itself, then multiply the result by N. This is just the hint. For numeric data, it makes no sense at all, but what to do if, for example, the operand is a matrix? If N is big, multiplying N times is not an option, Then the algorithm based on my hint is the way to go.

—SA


这篇关于在Python中实现指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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