File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,23 @@ fn to_str(mutable int n, uint radix) -> str
34
34
}
35
35
}
36
36
37
+ fn pow(int base, uint exponent) -> int {
38
+
39
+ if (exponent == 0u) {
40
+ ret 1;
41
+ } else if (base == 0) {
42
+ ret 0;
43
+ } else {
44
+ auto accum = base;
45
+ auto count = exponent;
46
+ while (count > 1u) {
47
+ accum *= base;
48
+ count -= 1u;
49
+ }
50
+ ret accum;
51
+ }
52
+ }
53
+
37
54
// Local Variables:
38
55
// mode: rust;
39
56
// fill-column: 78;
Original file line number Diff line number Diff line change @@ -11,6 +11,17 @@ fn test_to_str() {
11
11
check ( eq ( _int. to_str ( 100 , 10 u) , "100" ) ) ;
12
12
}
13
13
14
+ fn test_pow ( ) {
15
+ check ( _int. pow ( 0 , 0 u) == 1 ) ;
16
+ check ( _int. pow ( 0 , 1 u) == 0 ) ;
17
+ check ( _int. pow ( 0 , 2 u) == 0 ) ;
18
+ check ( _int. pow ( -1 , 0 u) == -1 ) ;
19
+ check ( _int. pow ( 1 , 0 u) == 1 ) ;
20
+ check ( _int. pow ( -3 , 2 u) == 9 ) ;
21
+ check ( _int. pow ( -3 , 3 u) == -27 ) ;
22
+ check ( _int. pow ( 4 , 9 u) == 262144 ) ;
23
+ }
24
+
14
25
fn main ( ) {
15
26
test_to_str ( ) ;
16
27
}
You can’t perform that action at this time.
0 commit comments