Skip to content

Commit

Permalink
Add nthOdd and nthEven method
Browse files Browse the repository at this point in the history
index starts with 0.
  • Loading branch information
xieweiking committed Oct 20, 2012
1 parent 585873b commit 9cecdd3
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/common/package.scala
Expand Up @@ -139,12 +139,21 @@ package object common {
@inline @inline
final def isEven(x: Int) = (x & 1) == 0 final def isEven(x: Int) = (x & 1) == 0


@inline
final def isOdd(x: Int) = !isEven(x)

@inline @inline
final def isEven(x: Long) = (x & 1L) == 0 final def isEven(x: Long) = (x & 1L) == 0


@inline
final def isOdd(x: Long) = !isEven(x)

@inline @inline
final def isEven(x: BigInt) = (x & BigInt(1)) == 0 final def isEven(x: BigInt) = (x & BigInt(1)) == 0


@inline
final def isOdd(x: BigInt) = !isEven(x)

@inline @inline
final def isInteger(x: Double) = x.asInstanceOf[Int].asInstanceOf[Double] == x final def isInteger(x: Double) = x.asInstanceOf[Int].asInstanceOf[Double] == x


Expand Down Expand Up @@ -174,7 +183,7 @@ package object common {


@inline @inline
final def combineAll(total: Int)(max: Int) = combineAny(total)(1, max) final def combineAll(total: Int)(max: Int) = combineAny(total)(1, max)

@inline @inline
final def powMod(a: Long, b: Long, m: Long) = { final def powMod(a: Long, b: Long, m: Long) = {
var r = 1L var r = 1L
Expand All @@ -186,7 +195,7 @@ package object common {
} }
r r
} }

@inline @inline
final def abMod(a: Long, b: Long, m: Long) = { final def abMod(a: Long, b: Long, m: Long) = {
var (rA, rB) = (a % m, b % m) var (rA, rB) = (a % m, b % m)
Expand All @@ -199,6 +208,16 @@ package object common {
r r
} }


@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 @inline
private final def sumOfAny(total: Int)(min: Int, max: Int)(calc: (Int, Int) => BigInt): BigInt = private final def sumOfAny(total: Int)(min: Int, max: Int)(calc: (Int, Int) => BigInt): BigInt =
if (min > max || min > total) 0 if (min > max || min > total) 0
Expand Down

0 comments on commit 9cecdd3

Please sign in to comment.