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

Asked

Viewed 325 times

1

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

I need to add them all up 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 the most times is the "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 appears most in the results..

Imprimir Resultados:

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 Verification of quantity:

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’s 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);
      }
    }
  }
}

Exit from the program:

inserir a descrição da imagem aqui

The output of the program would have to come out like this:

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)

Checking:

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

Upshot:

56*

UPDATING

My program sums up all the numbers of a given "sequence" with the sequence of numbers itself, and prints out a list of the results of each of them. The implementation of the program would be the verification of the results of all sums, ie, verify the amount of each result and print the amount of times that this result was obtained, thus determining, which was the result that appeared most times.

The program is divided into 3 Steps:

  1. Lists
  2. Checks
  3. Upshot

The 1st Stage is completed:

Established sequence: 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)

Already in the 2nd Stage I’m having some difficulties, I’m not getting to pass the Lista who is in Integer for int, an error occurs in this conversion and the check does not work.

In the program in check[z]++; was meant to be check[lista]++; I left the z which are just the sequence numbers, (7,14,21,28,35,42,49) just as an example, because I can’t get the conversion to work the verification.

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 ? "*" : ""));
    }      
  }
}

Exit:

inserir a descrição da imagem aqui

We saw that the 2nd Step is wrong, because the "list" is not placed to be checked, but the numbers of the sequence just as an example.

With the List at check (check[list]++;) the output of the program should be 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, in highlight and print the number:

Resultado Vencedor = 56*
  • I don’t understand what problem you’re trying to solve. I see it has to do with the multiplication table of 7 and the sum of digits, but I didn’t understand your statement.

  • Hello, Felipe. Welcome to [en.so]! Look, I confess I read it twice and I still don’t understand the goal. As far as I can ascertain, this is precisely the central problem here, that is, it is not a question of code, but of defining the problem correctly. This is something fundamental to programming: solve the problem first, then code. No problem defined, there is no way to define an algorithm and implement it correctly.

  • That said, I actually think you can understand, but the example with the sequence of multiples of 7 leads to understand that it is a simple arithmetic progression. What you really want is a square matrix whose rows and columns are the list numbers and the elements are the sum of the row and column number. Then you want to take the most common element. I see several mathematical applications of this, although I’m too rusty to remember the exact terminology.

2 answers

2

I was able to replicate your exit 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 don’t understand what the purpose of this program is or what problem you want to solve with it.

  • All right, Victor Stafusa, thanks for the answer.. but it still didn’t work, it would need to print first the "results" and then the "check" and so the "final result", also in the check occurred some errors, did not appear the number 14 and the 98 and also the number 56 appeared as a result 6 times which were actually to be 7 times...

  • The setting is from 1 to 7 but can be from 1 to 100 and so on... so it can be any number that the sequence takes itself and adds up these numbers and would print the lists and the checks and the final result... thus...

0

If understood, what you want is to make the combination of all numbers in the list with themselves, generating a kind of sum matrix, where each element is the sum of two numbers in the original list corresponding respectively to the row and column.

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

The secret is that you need to correctly store each sum and the amount of times it appears. You can’t do this with a simple variable.

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

See the full 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 made an additional, which is to display the numerical combinations that generated the most frequent sum. This ended up adding a bit of complexity to the code because I needed to store a list of such combinations on the map.

If you don’t need this, remove the bid from the list and use integer values on the map to represent the amount, instead of using the list size as I did.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.