Skip to content

Instantly share code, notes, and snippets.

@andreazevedo
Forked from mateusfreira/gist:3781896
Created September 25, 2012 17:57
Show Gist options
  • Save andreazevedo/3783432 to your computer and use it in GitHub Desktop.
Save andreazevedo/3783432 to your computer and use it in GitHub Desktop.
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)
1
else if ((currentResult > expectedResult) || (numbers.isEmpty))
0
else
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