Skip to content

Instantly share code, notes, and snippets.

@ochoto
Created September 25, 2012 09:07
Show Gist options
  • Save ochoto/3780757 to your computer and use it in GitHub Desktop.
Save ochoto/3780757 to your computer and use it in GitHub Desktop.
import scala.annotation.tailrec
object paresmayores extends App {
def mayoresPares(numPares: Int, lte: Int) = {
val startNum = if (lte % 2 == 0) lte else lte - 1
@tailrec
def mp(numPares: Int, start: Int, listaPares: List[Int]): List[Int] = {
if (numPares == 0)
listaPares
else
mp(numPares-1, start - 2, listaPares :+ start)
}
mp(numPares,startNum,List())
} //> mayoresPares: (numPares: Int, lte: Int)List[Int]
println(mayoresPares(3,6)) //> List(6, 4, 2)
println(mayoresPares(4,3)) //> List(2, 0, -2, -4)
println(mayoresPares(0,8)) //> List()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment