Skip to content

Commit d8cf356

Browse files
authored
Dana: Add powint program (#43)
* Add exponentiation by squaring in Dana * Rename powers to powint.dana and unify input/output with powint.alan
1 parent 05ecbdb commit d8cf356

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dana/programs/powint.dana

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def main
2+
3+
def powint: base as int, exp as int, mod as int, result as ref int
4+
5+
base := base % mod
6+
7+
if exp < 1:
8+
result := 1
9+
else:
10+
if exp % 2 == 0:
11+
var temp is int
12+
powint: base, exp / 2, mod, temp
13+
result := (temp * temp) % mod
14+
else:
15+
var temp is int
16+
powint: base, exp - 1, mod, temp
17+
result := (base * temp) % mod
18+
19+
var b is int
20+
var e is int
21+
var m is int
22+
var res is int
23+
24+
b := readInteger()
25+
e := readInteger()
26+
m := readInteger()
27+
28+
powint: b, e, m, res
29+
30+
writeInteger: res
31+
writeString: "\n"

0 commit comments

Comments
 (0)