Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
1.29/5 (6 votes)
See more:
how to split a number into smaller numbers so that their sum return the original number, all possible combinations
no is 5
OriginalGriff 10-Feb-14 14:25pm    
What have you tried?
Where are you stuck?
Vedat Ozan Oner 10-Feb-14 14:26pm    
seems like a homework??
OriginalGriff 10-Feb-14 14:39pm    
Yes - but it doesn't mean they don't need help.
We all had to start somewhere - as long as they do most of the work, there is nothing wrong with helping a bit.
Vedat Ozan Oner 10-Feb-14 14:58pm    
yes, I agree with you. Did I say 'do yourself' or something like that?
OriginalGriff 10-Feb-14 15:15pm    
Nope, didn't say you did. :laugh:
I just noticed that it had 5 "one" votes which is rather rude - not saying you voted on it - and your comment just seemed like a suitable place to remind some people that beginners need help too...

1 solution

The solution might be:

  1. Write the number as a serie of 1. (5 = 1+1+1+1+1) and put it in a proper variable (something like an array). We can call it seq.
  2. Sum the first two elements of the list ad put the result in another list if it doesn't already exist (neither itself or one of its permutations)
  3. If the procedure has only two numbers left, come back to the variable seq and sum the last two elements, go to 2, otherwise, if you have more than two elements left, go to 2 without doing anything.

To write the algorithm which checks for dobules in the final list, I suggest:

  • The final list must be a list of arrays
  • When you check for doubles, sort the elements of the current element of the list, without changing it (do it in a temporary array) and sort the one you've found too. If they contains the same elements in the same positions, it is already in the array, so do not add the result you've found to the list, otherwise, add it!!

To summarize up a bit you need:

  • A number which is the starting point (x).
  • An array which contains the current result (initial value: x elements set to 1)
  • An array which contains the initial point (it is modified at the point 3 of the procedure if only 2 items are left by the procedure. We can call this array Source)
  • A list which contains the results

You only have to sum the first element of the array to the second until you have only 2 elements and when this happens, come back to the array Source and sum the last two elements. Call the procedure you have done(sum the first two elements). Do it until you have two elements left in the Source array.

I hope it's helped to solve the problem.
Sorry for my bad English.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900