Created
September 25, 2012 09:07
-
-
Save ochoto/3780757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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