Skip to content

Instantly share code, notes, and snippets.

@renanreismartins
Created March 1, 2023 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renanreismartins/3d2ad063caa82be5c7c3f9406fde8ae4 to your computer and use it in GitHub Desktop.
Save renanreismartins/3d2ad063caa82be5c7c3f9406fde8ae4 to your computer and use it in GitHub Desktop.
move the stop to the beginning
object CanSum extends App {
def canSum(n: Int, coins: Array[Int]): Boolean = {
if (n == 0) return true
if (n < 0) return false
for (coin <- coins) {
val rest = n - coin
return canSum(rest, coins)
}
false
}
println(canSum(7, Array[Int](7)))
println(canSum(7, Array[Int](1)))
println(canSum(7, Array[Int](1, 7)))
println()
println(canSum(7, Array[Int](1, 2, 3, 8, 7)))
println(canSum(7, Array[Int]( 2, 3, 8, 7)))
println()
println(canSum(300, Array[Int](7, 14)))
println(canSum(7, Array[Int](8)))
println(canSum(7, Array[Int](6, 7)))
println(canSum(7, Array[Int](5, 3, 4, 7)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment