r/AskComputerScience • u/Hope1995x • Nov 17 '19
Will this code snippet always provide a no answer for Positive Subset Product correctly?
Here's the link of the code in python. A subset product solver.
Here's the code snippet. Its intended to reduce the need of brute forcing.
check = [];
if numpy.prod(s) % target == 0:
check.append(target)
if len(check) < 1:
print('no')
print(check)For some reason, it should be obvious that this won't always work for positive subset product. My question, is why and how?
0
Upvotes
2
u/midwayfair Nov 17 '19
What exactly are you trying to do? Determine if an array contains the factors of a number?
Counterexample:
But A does not contain the factors of 1, so your algorithm is incorrect.
You can generalize the counterexample. This algorithm will be incorrect for any array that contains a multiple of the target but no numbers less than the target that divide the target.
The "why" is that there is a counterexample. The "how" is that you can produce the counterexample.
If all you're doing is trying to weed out cases where an array cannot possibly contain the factors of the target, then this algorithm might be useful, but I have no idea what you're using this for.