0

I've found a few articles on something similar but I'm still so new to coding that I feel I'm getting more confused. I picked this project to work on so if anyone can help or point me in a direction you think can best explain it for me that would be helpful or be able to dissect the code in smaller pieces.

The goal User picks a number between 1-150 The code shows all combinations of numbers that sum up to equal that number but only using numbers between 1-50 and only using 5 digits. Also number can only be used once.

Example input is 15 Output: 1, 2, 3, 4, 5 This equals 15

If it was a higher number there would be more output combinations.

So general rules Array of 5 numbers (can only use numbers between 1-50 Can only use each number once Has to equal the input Also no duplicate arrays would be nice like 1,2,3,4,5 vs 5,1,4,2,3

I'm trying to write this using Kotlin or Java but anything should help, thanks!

J C
  • 13
  • 2
  • 1
    Hello, this seems like a homework question. To get help you can share your attempted approaches so far. – Vijay Jul 26 '18 at 06:32
  • Welcome to StackOverflow. This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level. You also need to state exactly what the problem is, what you expected, what you got, and any traceback. – Rory Daulton Jul 26 '18 at 08:30
  • You wrote "only using 5 digits." Did you mean "only using five numbers" or did you really mean there should be only 5 decimal digits among all the numbers? – Rory Daulton Jul 26 '18 at 10:08
  • C'mon, answer the question and show a little more of your work! I have an algorithm for you and can code it in Python. But I won't give you any more until you clarify the problem and show that you are not just dumping your homework on us. That can be done pretty easily. – Rory Daulton Jul 26 '18 at 13:22
  • @j-c This problem is called "integer partitions". A web search for that term should turn up some resources. – Robert Dodier Jul 26 '18 at 16:25
  • Thanks for the responses sorry for the delay been on vacation and military duty. First this is not homework, I'm trying to get into learning Java and I thought of a project I wanted to work on while learning to code in Java so I'm pretty new and also started taking a Udemy course and trying to answer other questions online. I meant 5 individual numbers. The user selects the number 55. The output will show unique number combinations that add up to 55 (using the specific range of numebrs 1-50) so for input of 55, one output might be 2, 5, 11, 17, 20 another would be 1, 4, 11, 12, 27 and so on. – J C Aug 06 '18 at 12:45

1 Answers1

0

Interesting challenge :)
I believe following link might be useful:

All you need to do is replace array with yours and the value '10' ( ... sum(seq) == 10]) at the end of the equation is gonna be replaced by your chosen number, like so:

import itertools 
numbers = [1, 2, 3, 7, 7, 9, 10, 11, 12, 13, 14, 15, 16]
chosen_number = 15
result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == chosen_number]
print result 
syntax-punk
  • 3,736
  • 3
  • 25
  • 33
  • Thanks, I think this points me in the right direction. Thank you so much. – J C Aug 13 '18 at 12:30
  • 1
    I found a website that actually does exactly what I'm trying to build from scratch. https://www.dcode.fr/partitions-generator Integer Number N = 20 Exactly M Number, M = 5 Only These Numbers: 1,2,3....... (1-20) for example Checked Only partitions without duplicates. It generated 7 results – J C Aug 13 '18 at 18:36