We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent edc3055 commit 8bb7d80Copy full SHA for 8bb7d80
examples/stdlib/stream/fastexp.effekt
@@ -43,6 +43,20 @@ def fastexp(n: Int, k: Int) = product {
43
}
44
45
def main() = {
46
+ /// Computes n^exp for integers. (from #923!)
47
+ def pow(n: Int, exp: Int): Int = {
48
+ def go(n: Int, exp: Int, acc: Int): Int = {
49
+ if (exp == 0) {
50
+ acc
51
+ } else if (mod(exp, 2) == 0) {
52
+ go(n * n, exp / 2, acc)
53
+ } else {
54
+ go(n * n, exp / 2, acc * n)
55
+ }
56
57
+ go(n, exp, 1)
58
59
+
60
def prettyBits(bits: List[Bool]): String =
61
"0b" ++ bits.map { b => if (b) "1" else "0"}.join("")
62
@@ -63,7 +77,7 @@ def main() = {
63
77
64
78
def testExp(n: Int, k: Int) = {
65
79
println(show(n) ++ " ^ " ++ show(k))
66
- println(n.toDouble.pow(k))
80
+ println(n.pow(k))
67
81
println(n.fastexp(k))
68
82
69
83
0 commit comments