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 0d43e90 commit 3219c40Copy full SHA for 3219c40
src/lib/int.rs
@@ -73,16 +73,19 @@ fn to_str(n: int, radix: uint) -> str {
73
fn str(i: int) -> str { ret to_str(i, 10u); }
74
75
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
- };
+ if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
+ if base == 0 { ret 0; }
+ let my_pow = exponent;
+ let acc = 1;
+ let multiplier = base;
+ while(my_pow > 0u) {
+ if my_pow % 2u == 1u {
+ acc *= multiplier;
+ }
+ my_pow /= 2u;
86
+ multiplier *= multiplier;
87
88
+ ret acc;
89
}
90
// Local Variables:
91
// mode: rust;
0 commit comments