Skip to content

Commit 1963939

Browse files
author
David Rajchenbach-Teller
committed
---
yaml --- r: 5767 b: refs/heads/master c: 3219c40 h: refs/heads/master i: 5765: 585070f 5763: 19954bc 5759: 2ce1f95 v: v3
1 parent a75db37 commit 1963939

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 0d43e90172c364a5f92a0f4447a5030274f18832
2+
refs/heads/master: 3219c40e18c20a0dbd41b106e1b05e3696bf4c85

trunk/src/lib/int.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,19 @@ fn to_str(n: int, radix: uint) -> str {
7373
fn str(i: int) -> str { ret to_str(i, 10u); }
7474

7575
fn pow(base: int, exponent: uint) -> int {
76-
ret if exponent == 0u {
77-
1
78-
} else if base == 0 {
79-
0
80-
} else {
81-
let accum = base;
82-
let count = exponent;
83-
while count > 1u { accum *= base; count -= 1u; }
84-
accum
85-
};
76+
if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
77+
if base == 0 { ret 0; }
78+
let my_pow = exponent;
79+
let acc = 1;
80+
let multiplier = base;
81+
while(my_pow > 0u) {
82+
if my_pow % 2u == 1u {
83+
acc *= multiplier;
84+
}
85+
my_pow /= 2u;
86+
multiplier *= multiplier;
87+
}
88+
ret acc;
8689
}
8790
// Local Variables:
8891
// mode: rust;

0 commit comments

Comments
 (0)