Java (Android) - Mathematical Equations

1

I need a little help. To get straight to the point, I was trying to create a small set of equations that would allow me to get a set of numbers with a few peculiarities. Basically what I wanted, after pressing a button, was to create four random numbers (from 1 to 9) that, together and subjected to certain mathematical equations, the final result was for example 22. Imagine: 8, 2, 5, 1 , are four random numbers that allow this equation "(8 * 2) + 5 + 1 = 22".

I know how to create the four random numbers and pass them to TextView's:

Random a = new Random();
int i1 = a.nextInt(10 - 1) + 1;
TextView tv = (TextView) findViewById(R.id.textView1);
Random b = new Random();
int i2 = b.nextInt(10 - 1)+1;
TextView tv2 = (TextView) findViewById(R.id.textView2);
Random c = new Random();
int i3 = c.nextInt(10 - 1)+1;
TextView tv3 = (TextView) findViewById(R.id.textView3);
Random d = new Random();
int i4 = d.nextInt(10 - 1)+1;
TextView tv4 = (TextView) findViewById(R.id.textView4);

Here are the various equations:

    int soma = i1 + i2 + i3 + i4;
    int mult0 = i1 * i2 * i3 * i4;
    int mult1 = (i1*i2*i3)+i4;
    int mult2 = (i1*i2)+i3+i4;
    int mult3 = i1 * (i2 + i3 + i4);
    int mult4 = i2 * (i1+i3+i4);
    int mult5 = i3 * (i1+i2+i4);
    int mult6 = i4 * (i1+i2+i3);
    int mult7 = (i1*i3)+i2+i4;
    int mult8 = (i1*i4)+i2+i3;
    int mult9 = (i2*i3)+i1+i4;
    int mult10 = (i2*i4)+i1+i3;
    int mult11 = (i4*i3)+i1+i2;
    int mult12 = (i1*i3*i4)+i2;
    int mult13 = (i1*i2*i4)+i3;
    int mult14 = (i4*i2*i3)+i1;

But the next step is to randomly pick one of the equations and get the set of random numbers i1, i2, i3, i4 in this equation to be 22, this I can not figure out how to do. I still have this code but it is not doing what I intend, (PS: TF = 22):

    if (soma == TF || mult0 == TF || mult1 == TF || 
mult2 == TF || mult3 == TF || mult4 == TF || mult5 == TF || 
mult6 == TF || mult7 == TF || mult8 == TF || mult9 == TF || 
mult10 == TF || mult11 == TF || mult12 == TF || mult13 == TF || mult14 == TF){
        tv.setText(String.valueOf(i1));
        tv2.setText(String.valueOf(i2));
        tv3.setText(String.valueOf(i3));
        tv4.setText(String.valueOf(i4));}

This code basically shows the four random numbers that obey one of these equations, but it is not automatically, it is by attempts (you need to press the button repeatedly until new numbers appear). I even pretended that whenever I hit the button, the right numbers would automatically appear on the first try.

Thanks if you can help me:)

    
asked by anonymous 11.07.2017 / 23:35

2 answers

0

Thank you very much Isac !! But I had to change a few things in your code .. Take a look to see:

@Override protected void onCreate (Bundle savedInstanceState) {         super.onCreate (savedInstanceState);         setContentView (R.layout.activity_main); }

int TF = 24;
Random b = new Random(); //apenas uma instancia de Random
int i1 = b.nextInt(10 - 1) + 1;
int i2 = b.nextInt(10 - 1) + 1;
int i3 = b.nextInt(10 - 1) + 1;
int i4 = b.nextInt(10 - 1) + 1;


public boolean verificarNumeros(){

    //construir as equações
    int[] mults = new int[]{ //agora em array para facilitar
            i1 + i2 + i3 + i4,
            i1 * i2 * i3 * i4,
            (i1*i2*i3)+i4,
            (i1*i2)+i3+i4,
            i1 * (i2 + i3 + i4),
            i2 * (i1+i3+i4),
            i3 * (i1+i2+i4),
            i4 * (i1+i2+i3),
            (i1*i3)+i2+i4,
            (i1*i4)+i2+i3,
            (i2*i3)+i1+i4,
            (i2*i4)+i1+i3,
            (i4*i3)+i1+i2,
            (i1*i3*i4)+i2,
            (i1*i2*i4)+i3,
            (i4*i2*i3)+i1
    };

    //e indicar se alguma se verificou
    for (int i=0; i < mults.length; ++i){
        if(mults[i] == TF){
            return true;
        }


        }

    return false;
    }



public void perform_action(View v){

    boolean combinacaoEncontrada = false;
    Random a = new Random(); //apenas uma instancia de Random



    while (!combinacaoEncontrada){
        i1 = a.nextInt(10 - 1) + 1;
        i2 = a.nextInt(10 - 1) + 1;
        i3 = a.nextInt(10 - 1) + 1;
        i4 = a.nextInt(10 - 1) + 1;
        combinacaoEncontrada = verificarNumeros();
    }

    //mostrar a combinação encontrada
    TextView tv = (TextView) findViewById(R.id.textView1);
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    TextView tv3 = (TextView) findViewById(R.id.textView3);
    TextView tv4 = (TextView) findViewById(R.id.textView4);

    tv.setText(String.valueOf(i1));
    tv2.setText(String.valueOf(i2));
    tv3.setText(String.valueOf(i3));
    tv4.setText(String.valueOf(i4));
}

}

    
12.07.2017 / 22:07
2

You can do a function that calculates all values and calls until you hit a valid result.

First you can isolate the check of four numbers in a separate method:

public boolean verificarNumeros(int i1, int i2, int i3, int i4){

    //construir as equações 
    int[] mults = new int[]{ //agora em array para facilitar
    i1 + i2 + i3 + i4,
    i1 * i2 * i3 * i4,
    (i1*i2*i3)+i4,
    (i1*i2)+i3+i4,
    i1 * (i2 + i3 + i4),
    i2 * (i1+i3+i4),
    i3 * (i1+i2+i4),
    i4 * (i1+i2+i3),
    (i1*i3)+i2+i4,
    (i1*i4)+i2+i3,
    (i2*i3)+i1+i4,
    (i2*i4)+i1+i3,
    (i4*i3)+i1+i2,
    (i1*i3*i4)+i2,
    (i1*i2*i4)+i3,
    (i4*i2*i3)+i1
    };

    //e indicar se alguma se verificou
    for (int i=0; i < mults.length; ++i){
         if(mults[i] == TF){
              return true;
         }
    }

    return false;
}

Now in the main method you can call this check method until you find a combination that works:

public void gerarNumeros(){
    boolean combinacaoEncontrada = false;
    Random a = new Random(); //apenas uma instancia de Random
    int i1,i2,i3,i4;

    while (!combinacaoEncontrada){
       i1 = a.nextInt(10 - 1) + 1;
       i2 = a.nextInt(10 - 1) + 1;
       i3 = a.nextInt(10 - 1) + 1;
       i4 = a.nextInt(10 - 1) + 1;
       combinacaoEncontrada = verificarNumeros(i1,i2,i3,i4);
    }

    //mostrar a combinação encontrada
    TextView tv = (TextView) findViewById(R.id.textView1);
    TextView tv2 = (TextView) findViewById(R.id.textView2);
    TextView tv3 = (TextView) findViewById(R.id.textView3);
    TextView tv4 = (TextView) findViewById(R.id.textView4);

    tv.setText(String.valueOf(i1));
    tv2.setText(String.valueOf(i2));
    tv3.setText(String.valueOf(i3));
    tv4.setText(String.valueOf(i4));
}

Recommendations:

    To generate 4 numbers from 0 to 8 it would be better to make a permutation between all possible numbers at the cost of for s or recursion because we can randomly process many more numbers and potentially not reach a solution!

  • In android and all other languages that have graphical interface, all heavy processing should not be done in the main thread , otherwise the system will look like it is not responding and locked up reach the result. To work around this effect you can use Thread or AsyncTask

12.07.2017 / 00:08