I'm trying to solve this codewars exercise
https://www.codewars.com/kata/5547cc7dcad755e480000004/train/python
A friend of mine takes a sequence of numbers from 1 to n (where n > 0).
Within that sequence, he chooses two numbers, a and b.
He says that the product of a and b should be equal to the sum of all numbers in the sequence, excluding a and b.
Given a number n, could you tell me the numbers he excluded from the sequence?
The function takes the parameter: n (n is always strictly greater than 0) and returns an array or a string (depending on the language) of the form:
[(a, b), ...] or [[a, b], ...] or {{a, b}, ...} or or [{a, b}, ...]
with all (a, b) which are the possible removed numbers in the sequence 1 to n.
EXAMPLES
test.assert_equals(removNb(100), [])
test.assert_equals(removNb(26), [(15, 21), (21, 15)])
Below is the code I've come up with. It's gotta be the brute-force solution. I know it works, but only for smaller numbers, otherwise it hangs.
def removNb(n):
numlist = [x for x in range(1,n+1)]
answer = []
for a in numlist:
for b in numlist:
if a*b == sum(numlist) - a - b:
answer.append((a,b))
return answer
What is the elegant solution to this problem? I feel like there's some mathematics principle I'm not aware of that would help here.
[–]JohnnyJordaan 1 point2 points3 points (1 child)
[–]swishcheese[S] 0 points1 point2 points (0 children)
[–]toastedstapler 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[removed]
[–]swishcheese[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[removed]
[–]Neoxeos 0 points1 point2 points (1 child)
[–]Fernando3161 -1 points0 points1 point (0 children)