Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find out all the possible combinations that can be formed using digits 1,2,3,4,5,6. Each set of combination should contain 24 digits in total. The summation of all the 24 digits should be 84.

Example: 6,5,5,5,6,3,2,5,4,1,1,2,2,2,3,4,3,5,5,6,3,2,3,1
The total number of digits in the above example is 24 and the summation of all the digits is 84

I have to print out off such possible combinations that can be formed in the quickest time possible.
Thanks!

What I have tried:

import java.lang.Math;
import java.lang.StringBuilder;

public class random24 {
    static char digits[] = {'1','2','3','4','5','6'};

    public static char randomDecimalDigit() {
        return digits[(int)Math.floor(Math.random() * 6)];
    }

    public static String randomDecimalString(int ndigits) {
        StringBuilder result = new StringBuilder();
        for(int i=0; i
Posted
Updated 10-Apr-22 10:17am
v2
Comments
Dave Kreskowiak 10-Apr-22 11:34am
   
If you want help with your code, you have to show your code.
Member 15597293 10-Apr-22 11:40am
   
import java.lang.Math;
import java.lang.StringBuilder;

public class random24 {
static char digits[] = {'1','2','3','4','5','6'};

public static char randomDecimalDigit() {
return digits[(int)Math.floor(Math.random() * 6)];
}

public static String randomDecimalString(int ndigits) {
StringBuilder result = new StringBuilder();
for(int i=0; i
Patrice T 10-Apr-22 12:19pm
   
Your code is corrupted in question.
try again to paste between <pre lang="C++"></pre> tags
If it don't work, paste again, select and use 'encode' button.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
Member 15597293 10-Apr-22 11:50am
   
Thanks for the reply.
I have already shared the code with which I was trying.

Thanks for the elaborate explanation. I guess it also does not cost any money to be polite to a stranger.
Quote:
I want to find out all the possible combinations that can be formed using digits 1,2,3,4,5,6. Each set of combination should contain 24 digits in total. The summation of all the 24 digits should be 84.
C++
public static char randomDecimalDigit() {
    return digits[(int)Math.floor(Math.random() * 6)];
}

Using random is a bad idea to find all possible combinations.

My solution would be :
- a recursive function.
- with a parameter to keep track of remaining digits to generate.
- with a parameter to keep track of remaining score (84) to fulfill.
- with a logic to ensure that the next digit is compatible with the constraint (84)
   
The following program finds the different arrangements (there are 1157) of the numbers 1,2,3,4,5,6 that produce valid sequences.
C++
#include <iostream>
#include <array>
#include <algorithm>
using namespace std;

enum { N = 24, TS = 84 };

void show(array<int, N> a)
{
  int sum = 0;
  for ( size_t n = 0; n<N; ++n)
  {
    sum += a[n];
  }
  if ( sum == TS )
  {
    for ( size_t n = 0; n<N; ++n)
    {
      cout << a[n] << " ";
    }
    cout << "\n";
  }
}

void recurse(int cur_digit, int cur_index, array<int,N> a)
{
  if ( cur_index == N)
  {
    show(a);
    return;
  }
  if ( cur_digit==6)
  {
    while ( cur_index < N)
    {
      a[cur_index++] = cur_digit;
    }
    show(a);
    return;
  }
  while (cur_index < N)
  {
    a[cur_index++] = cur_digit;
    recurse( cur_digit+1,  cur_index, a);
    for (int n= cur_index; n<N; ++n)
      a[n] = 0;
  }
}

int main()
{
  array<int, N> a{0};
  recurse(1, 0, a);
}

Excerpt of the output:
1 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
1 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 5
1 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 5 5
1 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5
1 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 6
1 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5
..
1 2 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 5 5 6

The solution your are looking for, is given by all the different permutations of each of the above lines. Finding such permutations it's the hard part of the task and it is up to you... :-)
   
Comments
Patrice T 10-Apr-22 16:23pm
   
Are you sure ?
Example: 6,5,5,5,6,3,2,5,4,1,1,2,2,2,3,4,3,5,5,6,3,2,3,1
In question, digit are not in a given order.
Here is my 20 first solutions
111111111111666666666666 1
111111111112566666666666 2
111111111112656666666666 3
111111111112665666666666 4
111111111112666566666666 5
111111111112666656666666 6
111111111112666665666666 7
111111111112666666566666 8
111111111112666666656666 9
111111111112666666665666 10
111111111112666666666566 11
111111111112666666666656 12
111111111112666666666665 13
111111111113466666666666 14
111111111113556666666666 15
111111111113565666666666 16
111111111113566566666666 17
111111111113566656666666 18
111111111113566665666666 19
111111111113566666566666 20
CPallini 10-Apr-22 16:39pm
   
The sequences my program finds are not the solution. Each sequence should be permutated. All the different permutations are the solution.
By the way, are you able to find ALL the sequences? I believe there is a huge number of them.
Patrice T 10-Apr-22 16:51pm
   
Absolutely huge number, indeed. :)
Rick York 10-Apr-22 18:49pm
   
Aren't there 6^24 possibilities? That's 4.738E18. Of course, not all will sum to 84.
CPallini 11-Apr-22 2:45am
   
In principle, you may actually compute the correct number of sequences summing up to 84: Take my 1157 'generators' and compute for each of them the number of distinct permutations. :-)
Rick York 11-Apr-22 14:34pm
   
I tried something a little different. I changed the number of digits to 12 and the sum target to 42, both half of the originals. This gave results that won't take an entire day to evaluate :
tests checked : 2,176,782,336
total matches :   144,840,476
elapsed time  :        12.222
the time is in seconds.
CPallini 11-Apr-22 16:20pm
   
Wow!
:thumbsup:
Rick York 11-Apr-22 20:07pm
   
FWIW, nothing about my algorithm is recursive. It uses a weird counting method I figured out for handling permutations. For this, it uses a vector of twenty-four counters that work with base 6 values. This allows it to use loops without recursing. I am going to see how it does with 16 digits and a target sum of 63 next. That should be interesting.
CPallini 12-Apr-22 1:56am
   
The Weird and Wonderful!
Rick York 10-Apr-22 21:30pm
   
I wrote a program to solve them. It ran for an hour and this is the last one it found : 29,480,000 : 111111111565415656666554. I didn't feel like letting it go for several more hours. There are going to be around 200M solutions in total I think.

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