Skip to content
Browse files

Add nthOdd and nthEven method

index starts with 0.
  • Loading branch information...
1 parent 585873b commit 9cecdd3e85786ce525cce7df4a402bbfd5fb9693 @xieweiking committed Oct 20, 2012
Showing with 21 additions and 2 deletions.
  1. +21 −2 src/common/package.scala
View
23 src/common/package.scala
@@ -140,12 +140,21 @@ package object common {
final def isEven(x: Int) = (x & 1) == 0
@inline
+ final def isOdd(x: Int) = !isEven(x)
+
+ @inline
final def isEven(x: Long) = (x & 1L) == 0
@inline
+ final def isOdd(x: Long) = !isEven(x)
+
+ @inline
final def isEven(x: BigInt) = (x & BigInt(1)) == 0
@inline
+ final def isOdd(x: BigInt) = !isEven(x)
+
+ @inline
final def isInteger(x: Double) = x.asInstanceOf[Int].asInstanceOf[Double] == x
@inline
@@ -174,7 +183,7 @@ package object common {
@inline
final def combineAll(total: Int)(max: Int) = combineAny(total)(1, max)
-
+
@inline
final def powMod(a: Long, b: Long, m: Long) = {
var r = 1L
@@ -186,7 +195,7 @@ package object common {
}
r
}
-
+
@inline
final def abMod(a: Long, b: Long, m: Long) = {
var (rA, rB) = (a % m, b % m)
@@ -200,6 +209,16 @@ package object common {
}
@inline
+ final def nthOdd(n: Int) = if (n <= 0) 1 else {
+ 2 * n + 1
+ }
+
+ @inline
+ final def nthEven(n: Int) = if (n <= 0) 0 else {
+ 2 * n
+ }
+
+ @inline
private final def sumOfAny(total: Int)(min: Int, max: Int)(calc: (Int, Int) => BigInt): BigInt =
if (min > max || min > total) 0
else {

0 comments on commit 9cecdd3

Please sign in to comment.
Something went wrong with that request. Please try again.