Java (Android) - Mathematical Equations

Asked

Viewed 124 times

1

I need a little help. To get to the point, I was trying to create a small system of equations that would allow me to get a set of numbers with a few small particularities. Basically what I intended, after pressing a button, was to create four random numbers (from 1 to 9) that, together and submitted 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, which is to randomly pick one of the equations and get the random number set I1, I2, i3, I4, in that equation to equal 22, that I can’t 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 several times until new numbers appear). I really intended that every time I pressed the button, the right numbers would automatically appear on the first try.

Thank you if you can help me :)

2 answers

2

You can make a function that calculates all values and go calling until you hit a valid result.

First you can isolate the verification 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 verification 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 would be better to make a permutation between all possible numbers at the expense of fors or recursion because we can randomly process many more numbers and potentially fail to come up with a solution!

  • In android and all other languages that have graphical interface, all heavy processing should not be done on thread main, otherwise the system will look like it is not responding and locked until the result is reached. To circumvent this effect you can use Thread or Asynctask

  • Thanks Isac!! I changed a few things and I got to what I wanted, it’s in the comments

0


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

@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));
}

}

Browser other questions tagged

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