]> git.lizzy.rs Git - rust.git/commitdiff
[Optim] int.rs: reimplemented pow with fast exponentiation
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Wed, 12 Oct 2011 08:22:26 +0000 (10:22 +0200)
committerDavid Rajchenbach-Teller <dteller@mozilla.com>
Wed, 12 Oct 2011 08:56:05 +0000 (10:56 +0200)
src/lib/int.rs

index 0a6e69ceadc7e3ad32280c057e61fdf08b1e711f..fe7689e3e28729fbc2d7e1edf4318f617dcb982a 100644 (file)
@@ -73,16 +73,19 @@ fn to_str(n: int, radix: uint) -> str {
 fn str(i: int) -> str { ret to_str(i, 10u); }
 
 fn pow(base: int, exponent: uint) -> int {
-    ret if exponent == 0u {
-            1
-        } else if base == 0 {
-            0
-        } else {
-            let accum = base;
-            let count = exponent;
-            while count > 1u { accum *= base; count -= 1u; }
-            accum
-        };
+    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;
+      multiplier *= multiplier;
+    }
+    ret acc;
 }
 // Local Variables:
 // mode: rust;