fork download
  1. object BigIntExt {
  2. implicit def bigintExt(n: BigInt) = new BigIntExt(n)
  3. }
  4. import BigIntExt._
  5. class BigIntExt(n : BigInt) {
  6. def **(pow: Int): BigInt =
  7. if (pow == 0) BigInt(1)
  8. else if ((pow % 2) == 0) {
  9. val x = n ** (pow / 2)
  10. x * x
  11. }
  12. else n * (n ** (pow - 1))
  13. }
  14.  
  15. object Main {
  16. def benchmark(name: String, fun: () => Any) = {
  17. val t1 = System.currentTimeMillis()
  18. val res = fun()
  19. val t2 = System.currentTimeMillis()
  20. println(name + ": "+(t2 - t1).toFloat/1000 + "sec; Result:"+res.toString())
  21. }
  22. def main (args: Array[String]) {
  23. val test_num = BigInt(2)
  24. val test_exp = 500000
  25.  
  26. benchmark("**", () => (test_num ** test_exp) % 1000000)
  27. benchmark("pow", () => (test_num pow test_exp) % 1000000)
  28. }
  29. }
Success #stdin #stdout 2.69s 247296KB
stdin
Standard input is empty
stdout
**: 1.188sec; Result:109376
pow: 1.252sec; Result:109376