-1

how can i make a calculation to say the sum of certain 4 numbers to give a certain result like 280. all possible combinations of numbers from 1 to 80. display all the combinations of 4 numbers that have the sum I set in the textbox. I should find out the easiest way to do this, and I would be grateful if you could help me. there should be no repetitions in a instance, like 60 60, 80 80. thus eliminating the unimportant variants, and the important ones will remain.

Textbox1.Text

68,69,70,72 = 280
67,69,70,74 = 280
66,69,70,75 = 280 
65,69,70,76 = 280
64,69,70,77 = 280

and so on...

I would need a model, how could I conceive such an algorithm? Thank you very much.

Get all combinations which declares a sum from a certain number

  • Divide the split of 4 into 2 say `A` and `B`. Now, further divide `A` into `A1` and `A2` and `B` into `B1` and `B2`. So, answer is `A1 + A2 + B1 + B2 = Number`. – nice_dev Dec 11 '19 at 15:50
  • 280/4 = 70, so we need 4 numbers whose average is 70, now starts with 70-i, 70+i, 70-j, 70+j (i & j are not same). since you limit 80, i or j should inside 10. – User_67128 Dec 11 '19 at 16:19
  • @ManojBanik - your proposed calculation won't create all the possible combinations. For instance, all five of the OP's samples won't be included in your calculations. – MarkL Dec 11 '19 at 17:37
  • 68-69-70-72 = 280?? I just wanted to give some hint, now you can furnish this idea to achieve your goal. – User_67128 Dec 11 '19 at 17:42
  • some other combinations like, 70, 70-i, 70-2*i, 70+3*i – User_67128 Dec 11 '19 at 17:44
  • there should be no confusion, I have modified - with (comma) " , " :) I think I understood the progress, or at least I think so, I'll try to see what I can do and back soon. I will also try to find out how to apply this. – Sandu Mihai Dec 11 '19 at 18:10
  • @SanduMihai if it helps, 80+80+80+40 shows that the smallest possible operand is 40. – djv Dec 11 '19 at 19:48
  • I understood, there is no problem, any kind of formula that creates such an algorithm helps me. I am willing to learn. – Sandu Mihai Dec 11 '19 at 20:03
  • When asking about homework (1) **Be aware of your school policy**: asking here for help may constitute cheating. (2) Specify that the question is homework. (3) **Make a good faith attempt** to solve the problem yourself first (include your code in your question). (4) **Ask about a specific problem** with your existing implementation; see [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). Also, [here](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) is guidance on asking homework questions. – Prune Dec 11 '19 at 22:19
  • Probable (though simplified) duplicate of https://stackoverflow.com/q/34517540/162313 – MarkL Dec 12 '19 at 00:53

1 Answers1

1

There may be a very large number of such numbers. Therefore I'm going to first get a concise data structure that describes it, then run code that executes on every one.

Since I don't have vb.net I will do it in Python. Hopefully the translation is not too hard. I have used closures and dictionaries. I did not use the natural Pythonic iterators.

#! /usr/bin/env python3

def find_combs_data (count, target, upper):
    # First we create a cache of known answers.
    cached = {}

    def find_combs (count_left, target_left, lower):
        cache_key = (count_left, target_left, lower)
        # Only do the calculation when needed.
        if cache_key not in cached:
            # Base case and sanity check.
            if count_left == 0:
                if target_left == 0:
                    return []
                else:
                    return None
            elif upper * count_left < target_left:
                return None
            elif target_left < lower * count_left:
                return None

            answer = {}
            for i in range(lower, upper + 1):
                result = find_combs(count_left - 1, target_left - i, i + 1)
                if result is not None:
                    answer[i] = result
            if len(answer):
                cached[cache_key] = answer
            else:
                cached[cache_key] = None

        return cached[cache_key]

    final = find_combs(count, target, 1)
    return final

def execute_on_combs (fn, count, target, upper):
    passed = []
    def execute_on_data (d):
        if d == []:
            fn(passed)
        else:
            for i, d_inner in d.iteritems():
                passed.append(i)
                execute_on_data(d_inner)
                passed.pop()
    execute_on_data(find_combs_data(count, target, upper))

def show (x):
    print(x)

execute_on_combs(show, 4, 270, 80)
btilly
  • 43,296
  • 3
  • 59
  • 88
  • This is helpful and for this algorithm, thank you, I will try the implementation in VB.Net and I will back if I have succeeded in applying. – Sandu Mihai Dec 11 '19 at 21:58