如何在 Clojure 中进行求幂?
如何在 Clojure 中进行求幂? 现在我只需要整数求幂,但问题也适用于分数。
How can I do exponentiation in clojure?
For now I'm only needing integer exponentiation, but the question goes for fractions too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
经典递归(看这个,它会破坏堆栈)
尾递归
函数
偷偷摸摸(也会破坏堆栈,但不那么容易)
库
classic recursion (watch this, it blows stack)
tail recursion
functional
sneaky (also blows stack, but not so easily)
library
Clojure 有一个运行良好的强大函数:我建议使用它而不是通过 Java 互操作,因为它可以正确处理所有 Clojure 任意精度数字类型。它位于命名空间 clojure.math.numeric-tower 中。
它被称为
expt
用于指数而不是power
或pow
这也许解释了为什么它有点难找到..无论如何,这是一个小例子(请注意use
有效,但最好使用require
):关于包安装的提醒
必须先安装Java包
org.clojure.math.numeric-tower
才能使Clojure命名空间clojure.math.numeric-tower
可访问!在命令行上:
然后编辑
project.clj
并将[org.clojure/math.numeric-tower "0.0.4"]
添加到依赖项向量。启动 lein REPL(不是 clojure REPL)
现在
:或者
Clojure has a power function that works well: I'd recommend using this rather than going via Java interop since it handles all the Clojure arbitrary-precision number types correctly. It is in namespace clojure.math.numeric-tower.
It's called
expt
for exponentiation rather thanpower
orpow
which maybe explains why it's a bit hard to find ... anyway here's a small example (note thatuse
works but better userequire
):Reminder about package installation
You must first install the Java package
org.clojure.math.numeric-tower
to make the Clojure namespaceclojure.math.numeric-tower
accessible!On the command line:
Then edit
project.clj
and add[org.clojure/math.numeric-tower "0.0.4"]
to the dependencies vector.Start a lein REPL (not a clojure REPL)
Now:
or
您可以使用 java 的
Math.pow
或BigInteger.pow
方法:You can use java's
Math.pow
orBigInteger.pow
methods:当这个问题最初被问到时, clojure.contrib .math/expt 是执行此操作的官方库函数。从那时起,它已移至 clojure.math.numeric-tower
When this question was originally asked, clojure.contrib.math/expt was the official library function to do this. Since then, it has moved to clojure.math.numeric-tower
从 Clojure 1.11 开始,
clojure.math/pow
附带 标准库,适用于 Clojure 和 ClojureScript。Since Clojure 1.11,
clojure.math/pow
ships with the standard library, and works both for Clojure and ClojureScript.如果您确实需要一个函数而不是方法,您可以简单地包装它:
并且在此函数中您可以将其转换为
int
或类似的。函数通常比方法更有用,因为您可以将它们作为参数传递给另一个函数 - 在这种情况下,我想到了map
。如果您确实需要避免 Java 互操作,您可以编写自己的强大函数。例如,这是一个简单的函数:
计算整数指数的幂(即无根)。
另外,如果您正在处理大数字,您可能需要使用
BigInteger
而不是int
。如果您正在处理非常大的数字,您可能希望将它们表示为数字列表,并编写自己的算术函数来在它们计算结果并将结果输出到其他某个时对它们进行流处理溪流。
If you really need a function and not a method you can simply wrap it:
And in this function you can cast it to
int
or similar. Functions are often more useful that methods because you can pass them as parameters to another functions - in this casemap
comes to my mind.If you really need to avoid Java interop, you can write your own power function. For example, this is a simple function:
That calculates power for integer exponent (i.e. no roots).
Also, if you are dealing with large numbers, you may want to use
BigInteger
instead ofint
.And if you are dealing with very large numbers, you may want to express them as lists of digits, and write your own arithmetic functions to stream over them as they calculate the result and output the result to some other stream.
我认为这也可行:
I think this would work too:
受 SICP 启发 完整迭代上面“偷偷摸摸”实现的快速版本。
SICP inspired full iterative fast version of 'sneaky' implementation above.
使用尾递归和支持负指数的“偷偷摸摸”方法的实现:
Implementation of "sneaky" method with tail recursion and supporting negative exponent:
使用reduce 的简单单行:
A simple one-liner using reduce:
使用
clojure.math.numeric-tower
,以前的clojure.contrib.math
。API 文档
Use
clojure.math.numeric-tower
, formerlyclojure.contrib.math
.API Documentation
请尝试
如果您想自己实现, 尾递归 O(log n) 解决方案(仅支持正整数)。显然,更好的解决方案是使用其他人指出的库函数。
Try
for a tail-recursive O(log n) solution, if you want to implement it yourself (only supports positive integers). Obviously, the better solution is to use the library functions that others have pointed out.
我个人使用:
注意星号后面的撇号 (')。
适用于所有大小的整数。
注意:对于某些实现来说,这可能会有点慢。
(time (pow 2 200000)) 在我的系统上花费了 1.2 秒来解析。
I personally use:
Notice the apostrophe (') after the asterisk.
Works well for all sizes of integers.
Note: This might be a little slow for some implementations.
(time (pow 2 200000)) took 1.2 seconds to resolve on my system.
clojure.contrib.genric.math-functions 怎么样
clojure.contrib.generic.math-functions 库中有一个 pow 函数。它只是 Math.pow 的一个宏,更多的是调用 Java 数学函数的“clojureish”方式。
http:// /clojure.github.com/clojure-contrib/generic.math-functions-api.html#clojure.contrib.generic.math-functions/pow
How about clojure.contrib.genric.math-functions
There is a pow function in the clojure.contrib.generic.math-functions library. It is just a macro to Math.pow and is more of a "clojureish" way of calling the Java math function.
http://clojure.github.com/clojure-contrib/generic.math-functions-api.html#clojure.contrib.generic.math-functions/pow