0

While practicing coding problems, I ran into this challenging problem:

Say I have a 7-variable equation, (A+B+C+C+D+B)(E+F+B+C)(G+F+F), and a huge list with up to 3500 numbers:

A 1, 2, 3; B 1, 2; C 9, 1; D 1; E 2; F 1; G 1

This list basically gives all the possible values that each variable can have. The question is, how many ways can I choose values of variables such that the resulting number is a multiple of seven?

For example, I could choose from the above list A = 1, B = 1, C = 1, D = 1, E = 2, F = 1, G = 1. This would make the number (1+2+1+1+1+1)(2+1+1+1)(1+1+1) = 35, which is indeed a multiple of seven.

My solution was to test every possible combination of the seven variables, and check if that sum was a multiple of seven. However, that solution is obviously very slow. Does anyone have a more efficient solution for this problem?

Bob Billy
  • 285
  • 1
  • 6

1 Answers1

0

while if the first number is a multiple of 7 than no matter what you times it by it well remain a multiple of 7

so (E+F+B+C)(G+F+F) is completely irrelevant.

if (E+F+B+C) is a multiple of 7 then the total is also a multiple of 7 same with (G+F+F).

If none of them are multiples of 7 than I don't think the answer can be a multiple of 7. At least, I cannot think of a case where this could happen. ex no amount of 10s will ever be devisible by 7 except say 7 or 14 10s

Joshua Byer
  • 524
  • 4
  • 11
  • Thanks! But how would I count the total possible number of answers? Could you explain a bit more with pseudocode or something? Thanks so much – Bob Billy Apr 04 '15 at 23:18
  • How would I implement this in an efficient way though? The only way I can think of is looping through everything again, which is basically the same as my solution. It's too slow. Thanks for the help though! – Bob Billy Apr 05 '15 at 00:36