Last active
July 17, 2020 15:53
-
-
Save marlonjames71/00014c40b9f85ee206d81db4f9d80489 to your computer and use it in GitHub Desktop.
Sum & Product Code Challenge
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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
and returns the smallest two positive integers x and y,
where x + y == sum, and x * y == product.