Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add nthOdd and nthEven method

index starts with 0.
  • Loading branch information...
commit 9cecdd3e85786ce525cce7df4a402bbfd5fb9693 1 parent 585873b
生猛哥 authored

Showing 1 changed file with 21 additions and 2 deletions. Show diff stats Hide diff stats

  1. +21 2 src/common/package.scala
23 src/common/package.scala
@@ -140,12 +140,21 @@ package object common {
140 140 final def isEven(x: Int) = (x & 1) == 0
141 141
142 142 @inline
  143 + final def isOdd(x: Int) = !isEven(x)
  144 +
  145 + @inline
143 146 final def isEven(x: Long) = (x & 1L) == 0
144 147
145 148 @inline
  149 + final def isOdd(x: Long) = !isEven(x)
  150 +
  151 + @inline
146 152 final def isEven(x: BigInt) = (x & BigInt(1)) == 0
147 153
148 154 @inline
  155 + final def isOdd(x: BigInt) = !isEven(x)
  156 +
  157 + @inline
149 158 final def isInteger(x: Double) = x.asInstanceOf[Int].asInstanceOf[Double] == x
150 159
151 160 @inline
@@ -174,7 +183,7 @@ package object common {
174 183
175 184 @inline
176 185 final def combineAll(total: Int)(max: Int) = combineAny(total)(1, max)
177   -
  186 +
178 187 @inline
179 188 final def powMod(a: Long, b: Long, m: Long) = {
180 189 var r = 1L
@@ -186,7 +195,7 @@ package object common {
186 195 }
187 196 r
188 197 }
189   -
  198 +
190 199 @inline
191 200 final def abMod(a: Long, b: Long, m: Long) = {
192 201 var (rA, rB) = (a % m, b % m)
@@ -200,6 +209,16 @@ package object common {
200 209 }
201 210
202 211 @inline
  212 + final def nthOdd(n: Int) = if (n <= 0) 1 else {
  213 + 2 * n + 1
  214 + }
  215 +
  216 + @inline
  217 + final def nthEven(n: Int) = if (n <= 0) 0 else {
  218 + 2 * n
  219 + }
  220 +
  221 + @inline
203 222 private final def sumOfAny(total: Int)(min: Int, max: Int)(calc: (Int, Int) => BigInt): BigInt =
204 223 if (min > max || min > total) 0
205 224 else {

0 comments on commit 9cecdd3

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