How to check which result will appear most often in a chronological sum of an established sequence?

1

I have this sequence of numbers: 7, 14, 21, 28, 35, 42, 49 , being able to have more or less numbers.

I need to add them all as follows:

7+7=14  
14+7=21  
21+7=28  
28+7=35  
35+7=42  
42+7=49  
49+7=56  

That was 7, the others are missing. All + 14, all + 21, all + 28, so on ...

Example:

7=(14,21,28,35,42,49,56)  
14=(21,28,35,42,49,56,63)  
21=(28,35,42,49,56,63,70)  
28=(35,42,49,56,63,70,77)  
35=(42,49,56,63,70,77,84)  
42=(49,56,63,70,77,84,91)  
49=(56,63,70,77,84,91,98)  

These are the results of each sum of all numbers, and the number that appeared most often is "56" ie:

7+49=56  
14+42=56  
21+35=56  
28+28=56  
35+21=56  
42+12=56  
49+7=56  

I need to print these results in lists and check the number of numbers to get the number that most appears in the results.

Print Results:

7=(14,21,28,35,42,49,56)  
14=(21,28,35,42,49,56,63)  
21=(28,35,42,49,56,63,70)  
28=(35,42,49,56,63,70,77)  
35=(42,49,56,63,70,77,84)  
42=(49,56,63,70,77,84,91)  
49=(56,63,70,77,84,91,98) 

Print Quantity Check:

14=1  
21=2  
28=3  
35=4  
42=5  
49=6  
56=7*   
63=6  
70=5  
77=4  
84=3  
91=2  
98=1  

Print Final Result:

The sum with the highest result is the number "56".

I tried this code but it is not working:

public static void main(String[] args) {

for (long a=1; a <= 7; a++) {

long z;
z=a*7;

for (long x = z; x <= z; x++) {

long y;
y=z+x;

System.out.println(x+"="+y);
      }
    }
  }
}

Program exit:

Theoutputoftheprogramwouldhavetogolikethis:

Lists:

7=(14,21,28,35,42,49,56)14=(21,28,35,42,49,56,63)21=(28,35,42,49,56,63,70)28=(35,42,49,56,63,70,77)35=(42,49,56,63,70,77,84)42=(49,56,63,70,77,84,91)49=(56,63,70,77,84,91,98)

Verification:

14=121=228=335=442=549=656=7*63=670=577=484=391=298=1

Result:

56*

UPDATE

Myprogramsumsallnumbersinagiven"sequence" with the sequence of numbers themselves, and prints a list of the results of each sequence. The implementation of the program would be to verify the results of all the sums, that is, to verify the quantity of each result and to print the amount of times that this result was obtained, thus determining, what was the result that appeared more often. p>

The program is divided into 3 stages:

  • Lists
  • Checks
  • Result
  • Step 1 is complete:

    Sequence established: 7,14,21,28,35,42,49 .

    1) Lists

    7=(14,21,28,35,42,49,56)   
    14=(21,28,35,42,49,56,63)   
    21=(28,35,42,49,56,63,70)   
    28=(35,42,49,56,63,70,77)   
    35=(42,49,56,63,70,77,84)   
    42=(49,56,63,70,77,84,91)   
    49=(56,63,70,77,84,91,98)
    

    In Step 2, I'm having some difficulties, I'm not able to pass Lista that is in Integer to int , an error occurs in this conversion and does not work verification.

    In program% w / o% was to be% w /% I left% w /% w which are only sequence numbers, (7,14,21,28,35,42,49) as an example only, because I get the conversion for the verification to work.

    Program:

    package etapa2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Etapa2 {
    
    private List<Integer> lista = null;
    
    public Etapa2(List<Integer> lista) {
        this.lista = lista;
    }
    
    public void soma() {
        for (Integer externo : lista) {
            System.out.print(externo + "=");
            List<String> resultado = new ArrayList<String>();
            for (Integer interno : lista) {
                resultado.add(String.valueOf(interno + externo));
            }
            System.out.println("("+String.join(",", resultado)+")");  
        }
    }
    
    public static void main(String args[]) {
    
    List<Integer> lista = new ArrayList<>();
    
      int[] check = new int[100000000];
    
      for (int f=1; f<=7; f++){
    
          int z,p;
          z=f*7;
          lista.add(z);
          check[z]++;
        }
        Etapa2 sl = new Etapa2(lista);
        sl.soma();
    
        System.out.println("------------");
    
        int maior = 0;
        for (int i : check) {
            if (i > maior) maior = i;
        }
        for (int j = 0; j <= 1000000; j++) {
            if (check[j] == 0) continue;
    
            System.out.println(j + "=" + check[j] + (check[j] == maior ? "*" : ""));
        }      
      }
    }
    

    Output:

    Wehaveseenthatthe2ndStepiswrong,becausethe"list" to be checked is not placed, but the sequence numbers are just an example.

    With the checklist (check [list] ++;) the output of the program should look like this:

    7=(14,21,28,35,42,49,56)   
    14=(21,28,35,42,49,56,63)   
    21=(28,35,42,49,56,63,70)   
    28=(35,42,49,56,63,70,77)   
    35=(42,49,56,63,70,77,84)   
    42=(49,56,63,70,77,84,91)   
    49=(56,63,70,77,84,91,98)  
    
    14=1   
    21=2   
    28=3   
    35=4   
    42=5   
    49=6   
    56=7*   
    63=6   
    70=5  
    77=4   
    84=3   
    91=2   
    98=1  
    

    And the Final Step, put the result that appeared more often, highlight and print the number:

    Resultado Vencedor = 56*
    
        
    asked by anonymous 12.06.2017 / 21:19

    2 answers

    2

    I was able to replicate your output and your step-by-step with this:

    class Somas7 {
        public static void main(String[] args) {
            int[] histograma = new int[100];
            for (int a = 1; a < 7; a++) {
                for (int b = 1; b < 7; b++) {
                    histograma[7 * (a + b + 1)]++;
                }
            }
            int maior = 0;
            for (int i : histograma) {
                if (i > maior) maior = i;
            }
            for (int j = 0; j < 100; j++) {
                if (histograma[j] == 0) continue;
                System.out.println(j + "=" + histograma[j] + (histograma[j] == maior ? "*" : ""));
            }
        }
    }
    

    See here working on ideone.

    However, honestly, I do not understand what the purpose of this program is or what the problem is that you want to solve with it.

        
    12.06.2017 / 21:37
    0

    If you understand, you want to combine all the numbers in the list with themselves, generating a sort of sum array, where each element is the sum of two numbers in the original list corresponding respectively to the line and the column.

    Then just find the most common element of the array, which is nothing more than the most common sum.

    The secret is that you need to properly store each sum and the number of times it appears. You can not do this with a simple variable.

    For this, I made a solution using a map, where each entry in the map is a sum and the value of that entry is the combination of numbers that totaled the sum.

    See the complete code:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class MatrixFrequentElement {
        public static void main(String[] args) {
            //the numbers, duh
            int[] numbers = {7, 14, 21, 28, 35, 42, 49};
    
            //print initial matrix with sums
            printSumMatrix(numbers);
            System.out.print("\n--\n");
    
            //compute sums and frequency
            Result r = findMostFrequentSum(numbers);
    
            //print number of occurrences for each matrix entry
            printQuantities(r);
            System.out.print("\n--\n");
    
            //print combinations which resulted in the most frequent sum
            printSumsCombinations(r);
            System.out.print("\n--\n");
    
            //final result
            System.out.printf("Final result: " + r.mostFrequentNumber);
    
        }
    
        /**
         * Stores the multivalued result.
         */
        static class Result {
            final Map<Integer, List<String>> sumsByTotal;
            final int mostFrequentNumber;
    
            Result(Map<Integer, List<String>> sumsByTotal, int mostFrequentNumber) {
                this.sumsByTotal = sumsByTotal;
                this.mostFrequentNumber = mostFrequentNumber;
            }
        }
    
        /**
         * Compute sums of each element in the received list with all elements of the same list (including the very element)
         * and stores the most frequent sum and how many occurrences were found.
         */
        public static Result findMostFrequentSum(final int[] numbers) {
            //sorted map whose value is the sum and value is a list of the original values which totalized the number
            final Map<Integer, List<String>> sumsByTotal = new TreeMap<>();
            final int l = numbers.length;
            int mostFrequentNumber = -1;
            int mostFrequentLength = -1;
            for (int i = 0; i < l; i++) {
                for (int j = 0; j < l; j++) {
                    final int s = numbers[i] + numbers[j];
                    //get list of sums from map (or create a new one when it does not exist)
                    final List<String> sums = sumsByTotal.computeIfAbsent(s, k -> new ArrayList<>());
                    //add current sum
                    sums.add(String.format("%d+%d", numbers[i], numbers[j]));
                    //check frequency and stores most frequent
                    if (mostFrequentLength == -1 || mostFrequentLength < sums.size()) {
                        mostFrequentNumber = s;
                        mostFrequentLength = sums.size();
                    }
                }
            }
            return new Result(sumsByTotal, mostFrequentNumber);
        }
    
        /**
         * Prints each unique entry of the matrix with the number of times it showed up.
         */
        public static void printQuantities(Result r) {
            for (Map.Entry<Integer, List<String>> e :  r.sumsByTotal.entrySet()) {
                System.out.printf("%d=%d", e.getKey(), e.getValue().size());
                if (e.getKey() == r.mostFrequentNumber) {
                    System.out.print("*");
                }
                System.out.println();
            }
        }
    
        /**
         * Prints all the combinations which resulted in the most frequent sum.
         */
        public static void printSumsCombinations(Result r) {
            for (String sum : r.sumsByTotal.get(r.mostFrequentNumber)) {
                System.out.printf("%s=%d%n", sum, r.mostFrequentNumber);
            }
        }
    
        /**
         * Prints a matrix whose values are a sum of two numbers from the received list corresponding to the row and column.
         */
        public static void printSumMatrix(final int[] numbers) {
            final int l = numbers.length;
    
            //print first empty cell
            System.out.print("+\t\t");
            //print row with headers
            for (int i = 0; i < l; i++) {
                System.out.printf("%d\t", numbers[i]);
            }
            System.out.println();
            //separating row
            System.out.print("\t\t");
            for (int i = 0; i < l; i++) {
                System.out.print("--\t");
            }
            System.out.println();
            //print other rows
            for (int i = 0; i < l; i++) {
                //first column is a header as well
                System.out.printf("%d\t=\t", numbers[i]);
                //print rest ot the row with sums
                for (int j = 0; j < l; j++) {
                    final int s = numbers[i] + numbers[j];
                    System.out.printf("%d\t", s);
                }
                System.out.println();
            }
        }
    }
    

    The output generated is:

    +       7   14  21  28  35  42  49  
            --  --  --  --  --  --  --  
    7   =   14  21  28  35  42  49  56  
    14  =   21  28  35  42  49  56  63  
    21  =   28  35  42  49  56  63  70  
    28  =   35  42  49  56  63  70  77  
    35  =   42  49  56  63  70  77  84  
    42  =   49  56  63  70  77  84  91  
    49  =   56  63  70  77  84  91  98  
    
    --
    14=1
    21=2
    28=3
    35=4
    42=5
    49=6
    56=7*
    63=6
    70=5
    77=4
    84=3
    91=2
    98=1
    
    --
    7+49=56
    14+42=56
    21+35=56
    28+28=56
    35+21=56
    42+14=56
    49+7=56
    
    --
    Final result: 56
    

    Note that I did an additional, which is to display the numeric combinations that generated the most frequent sum. This ended up adding a bit of code complexity because I needed to store a list with such combinations on the map.

    If you do not need this, remove the bid from the list and use integer values on the map to represent the amount, rather than using the list size as I did.

        
    28.06.2017 / 09:29