Skip to content

Instantly share code, notes, and snippets.

@marlonjames71
Last active July 17, 2020 15:53
Show Gist options
  • Save marlonjames71/00014c40b9f85ee206d81db4f9d80489 to your computer and use it in GitHub Desktop.
Save marlonjames71/00014c40b9f85ee206d81db4f9d80489 to your computer and use it in GitHub Desktop.
Sum & Product Code Challenge
func sumAndProduct(sum: Int, product: Int) -> [Int] {
var nums: [Int] = []
guard sum > 0 && product > 0 else { return nums }
for num1 in 1...sum {
for num2 in num1...sum {
if num1 + num2 == sum && num1 * num2 == product {
nums.append(contentsOf: [num1, num2])
}
}
}
return nums
}
@marlonjames71
Copy link
Author

  • Write a function that takes two positive integers, a sum and a product,
    and returns the smallest two positive integers x and y,
    where x + y == sum, and x * y == product.
  • Return x and y in an array with the smaller number first in the format [x, y]
  • If a solution cannot be found, return the empty array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment