Created
September 25, 2014 13:55
-
-
Save modalsoul/85220d8e4d63d9eac11a to your computer and use it in GitHub Desktop.
Partial Sum Problem (n < 20)
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
def partialSum(n:Int, a:Seq[Int], k:Int, i:Int = 0, sum:Int = 0):Boolean = { | |
if(i == n) sum == k | |
else partialSum(n, a.tail, k, i+1, sum+a.head) || partialSum(n, a.tail, k, i+1, sum) | |
} | |
val a = Seq(1,2,4,7) | |
println(partialSum(a.length, a, 13)) | |
println(partialSum(a.length, a, 15)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment