y u no do it?

Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

Starts at just \$1 per CPM or \$0.10 per CPC.

# Generate all permutations of a group of numbers that add up to a sum?

Discussion in 'Programming' started by MindReality, Nov 25, 2019.

Not open for further replies.
1. #1
Hi,

I would like to generate all permutations of the numbers 1 to 26 that add up to a sum value, but limited to a certain series.

For example, if I input the sum value of 40 with the series of 4, it should display permutations of the numbers 1 to 26 such as:

1+1+12+26
1+1+13+25
1+1+14+24
Etc...

I would like the permutation series to go only in ascending sequence from left to right so that the same series of numbers do not repeat itself.

For e.g. 1+1+12+26 is essentially the same as 26+12+1+1

So let's say the sequence reaches 1+1+18+20 followed by 1+1+19+19
Then the next sequence should be: 1+2+18+19
instead of 1+1+20+18 (which is essentially a repeat of 1+1+18+20)

Is there a simple way of coding this?

Nov 25, 2019 IP
2. ### sarahkiTamerStaff

Messages:
28,142
4,388
123
Trophy Points:
665
#2
Yep, have nested while loops.

This is obviously homework - we'll critique your strategy and your code but we're not going to do it for you.

Nov 25, 2019 IP
3. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#3
Are there any examples or scripts out there?

We looked at these but we can't quite get it to work:

https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum

https://stackoverflow.com/questions/10738926/efficient-algorithm-to-find-a-combination-which-summation-is-equal-to-a-known-n

https://stackoverflow.com/questions/19437855/algorithm-for-finding-a-group-of-numbers-in-a-list-that-equal-a-target

Nov 26, 2019 IP
4. ### sarahkiTamerStaff

Messages:
28,142
4,388
123
Trophy Points:
665
#4
Seriously?
This is so basic that I'm not surprised you can't find examples.
Write one version
Write another
Look at the strengths of each
Rewrite one of them
Check it
Submit it.

Nov 26, 2019 IP
5. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#5
My programmer mentioned that to run a check on the numbers 1 to 26 that add up to a sum value with 6 numbers, it would be 6 to the power of 26 which is a very high number for a server to handle.

What would be a better way to go about doing what I mentioned in my original post?

Nov 26, 2019 IP
6. ### mmerlinnProminent Member

Messages:
3,202
820
7
Trophy Points:
320
#6
This is so ridiculously simple that I question why you even ask. You know exactly what you start with and what you want to end with. You could almost eyeball it and get it done without a program.

What your question tells me is that you are not a programmer. In fact, you might even have a problem understanding simple math as this entails nothing more than addition and subtraction.

Nov 26, 2019 IP
7. ### sarahkiTamerStaff

Messages:
28,142
4,388
123
Trophy Points:
665
#7
And won't pass this course if s/he isn't willing to put some effort in.

Nov 26, 2019 IP
mmerlinn likes this.
8. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#8
Your comment is an uncalled for act of verbal abuse.

Nov 26, 2019 IP
9. ### sarahkiTamerStaff

Messages:
28,142
4,388
123
Trophy Points:
665
#9
Not really. We're all wondering why you don't just mess about with some basic while loops and get the job done. I reckon it would take 10 or 20 minutes - much less than the 1 hour lab time you've probably got allocated for it.

Have you got any code to share yet? Like, have you even made a start?

Nov 26, 2019 IP
10. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#10
This comment is not helpful either. I told you I am not a programmer. I am looking for help on behalf of my programmer who is doing a software for me. In my previous thread I have received real help from someone. Those are the kind of input that is truly helpful on this forum.

Nov 26, 2019 IP
11. ### tuxandrewActive Member

Messages:
63
9
0
Trophy Points:
68
#11
Used python for loop. Would this be something helpful for you request?

first=1
second=1
l=40
for i in range(3,l+1):
for j in range(3,l+1):
while i+j+second+first==l:
print(first,"+",second,"+",i,"+",j,"=",l)
second=second+1
j=j-1
break

Last edited: Nov 27, 2019
Nov 27, 2019 IP
12. ### sarahkiTamerStaff

Messages:
28,142
4,388
123
Trophy Points:
665
#12
We find it unfathomable that this isn't a beginner's homework assignment. If you "have a programmer" presumably you are paying that person and they should be able to cope with a beginner's assignment.

Maybe if we understood the business need behind this we might take it seriously, until then I suspect you're going to get "do your homework" kinds of answers (apart from @tuxandrew)

Nov 27, 2019 IP
13. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#13
In my first post, I have described the function I want in terms of numbers, but it is a step to something bigger. I created a new thread with a title that starts at the macro picture of what I want and the details of it, but you merged it into another thread of mine. That is not right. The previous thread has already been solved and I already assigned a best answer. I have already managed to create that function successfully. Now I am looking to create a different function which is more complex that has yet to be solved.

Previously, I was looking to derive words from an existing word list that add up to a sum value.

This time, I am looking to form all possible combinations of letters that add up to a sum value. For example, AABCD is not a word. It is just a combination of letters. With it, I can manually form new words like BADAC. This gives ideas for company names or product names that do not exist yet.

I will post my macro picture description of this function below. By right it should form a completely new thread, new subject and OP to make it the focal point.

Last edited: Nov 28, 2019
Nov 28, 2019 IP
14. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#14
Below is what I want to achieve with the above description. This is a different function from a previous thread of mine which already has been solved. The new function involves combination of letters, not word lists.

Subject: Generate All Combination of Alphabets that Add up to a Sum Value?

How can I generate all combinations of alphabets that add up to a sum value?

A=1,B=2,C=3,etc...

So if I input the sum value of "24", I would like it generate all combination of letters in:
1. Ascending Sequence only so no sequence will repeat itself.
2. Fixed number of letters of my choosing.

So, if I input the sum value of "24" and the number of letters as "3",

It should generate a list of alphabet combinations including the word:

"ACT"

Because 24 = 1+3+20 = ACT

I don't need it to display TAC or CAT because they are the same combination as ACT.

This will reduce the total amount of combinations needed to be worked with, prevent duplicates and save processing power, etc.

I am not a programmer btw. I am creating a software but I need help to know the proper programming logic for it and how this can be done so that I can work with a programmer to create this function for me.

This is for numerology purposes. The objective is to be able to generate all possible permutations of alphabets that add up to a certain sum value so that I can use those groups of letters and manually arrange them to form new words or names that don't exist yet that add up to that particular numerological sum value.

Nov 28, 2019 IP
15. ### Mike2718Greenhorn

Messages:
46
5
0
Trophy Points:
23
#15
here is a very high level example of what you need:
``````StringBuffer res = new StringBuffer();
for (int i=1; i<26; i++){
for (int j=1; j<26; j++){
for (int k=1; k<26; k++){
for (int l=1; l<26; l++){
if (num[I] + num[j] + num[k] +  num[l] ==40)
res.add(num[I] +',' + num[j]+',' + num[k]+',' + num[l]+"\n")
}
}
}
}
return res;``````
Code (javascript):
[/I][/I]

Last edited by a moderator: Dec 3, 2019
Dec 3, 2019 IP
scu8a likes this.
16. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#16
Hi, thanks for this. We have tried something similar and this is the issue we come across:

"With this code, each loop is running 26 times, so 4 loops makes: 26 x 26 x 26 x 26 total over 400,000 combinations. If we add one more loop, it becomes 11 millions... (5 max length) If we add 2 loops it goes into billions (6 max length) Until 4 loops, there is no problem at all. Problem starts at 6 loops or more..."

Problem with server crashing due to too many computations unless have a way to eliminate unnecessary loops by making the permutation series to go only in ascending sequence from left to right so that the same series of numbers do not repeat itself. We are trying to find a way to do this.

Dec 5, 2019 IP
17. ### scu8aGreenhorn

Messages:
81
20
0
Trophy Points:
15
#17
When I see the number 26, I get the impression that we're working with the alphabet. When I look at 26^x I think "that could be a password cracking script"

But, why just limit yourself to alphabet characters?

Isn't the minimum password length standard generally accepted to be 6 characters long at the least? Huh, that's odd.

Send me a private message with your programmer's number so I communicate with him directly. It will only take a few seconds and the problem is solved.

Good idea. Check the server logs linked to activity from MindReality. Maybe that way we can get a better idea of what the problem is and get it resolved. He's not being treated nicely and I don't think that's good.

LMAO!

Neither is this one. Do you have an attorney?

Last edited by a moderator: Dec 18, 2019
Dec 18, 2019 IP
18. ### mmerlinnProminent Member

Messages:
3,202
820
7
Trophy Points:
320
#18
Have your "programmer" write out the sequence LONGHAND, then convert it to code. It is as simple as that. If your "programmer" is not capable of such a simple exercise, FIRE him and HIRE a REAL programmer.

Dec 18, 2019 IP
sarahk likes this.
19. ### MindRealityWell-Known Member

Messages:
202
5
0
Trophy Points:
105
#19
You have the right to remain an attorney...

Dec 18, 2019 IP

Messages:
202