Skip to content

Instantly share code, notes, and snippets.

Forked from mateusfreira/gist:3781896
Created September 25, 2012 17:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Dado um vetor com 'n' números distintos, calcular quantas possibilidades existem para se obter a soma 's'
def countSum(numbers: List[Int], expectedResult: Int): Int = {
countSumRecursively(numbers, 0, expectedResult);
def countSumRecursively(numbers: List[Int], currentResult: Int, expectedResult: Int): Int = {
if (currentResult == expectedResult)
else if ((currentResult > expectedResult) || (numbers.isEmpty))
countSumRecursively(numbers, currentResult + numbers.head, expectedResult) + countSumRecursively(numbers.tail, currentResult, expectedResult);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment