How to print only numbers that repeat between two different formulas?

Asked

Viewed 85 times

1

I have two 50 number formulas.
One has multiples of 3 and the other multiples of 7.

Code:

public static void main(String[] args) {           
    BigInteger start = new BigInteger("1");
    BigInteger limit = new BigInteger("50");
    BigInteger n1 = new BigInteger("3");
    BigInteger n2 = new BigInteger("7");

    for (BigInteger a = start; a.compareTo(limit) <= 0; a = a.add(BigInteger.ONE)) {              
        BigInteger copas3 = a.multiply(n1); 
        BigInteger copas7 = a.multiply(n2);

        System.out.println( copas3 );
        System.out.println( copas7 );      
    }
}

See working on repl: https://repl.it/repls/AngryImpureSequences

I need only the repeating numbers between the two formulas to be printed.
So just:

21
42
63
84
105
126
147
  • You want only with these codes or we can suggest different applications?

  • It can be with different applications, but needs to be Biginteger and without Lists...

1 answer

3


A considerably simple solution is to use two variables in the for one for the copas3 and another to the copas7, instead of being the variable a for both of them. So it can always increase the one that is lower to give the possibility of both being equal:

public static void main(String[] args) {

    BigInteger start = new BigInteger("1");
    BigInteger limit = new BigInteger("50");
    BigInteger n1 = new BigInteger("3");
    BigInteger n2 = new BigInteger("7");

    for (BigInteger a=start,b=start;a.compareTo(limit)<=0 && b.compareTo(limit) <= 0;){ 
        BigInteger copas3 = a.multiply(n1); //contador "a" apenas para o copas3
        BigInteger copas7 = b.multiply(n2); //contador "b" apenas para o copas7

        int comparacao = copas3.compareTo(copas7); 
        if (comparacao == 0) { //escreve apenas se forem iguais
            System.out.println(copas3);
        }

        //aumenta apenas o mais pequeno
        if (comparacao <= 0) { 
            a = a.add(BigInteger.ONE);
        }
        else {
            b = b.add(BigInteger.ONE);
        }
    }
}

See the result you were looking for in Ideone

  • Hello @Isac all right? So the code is good but unfortunately it didn’t work, there is another way? When trying to make a progressive sum in the formula, for example "2+4+6+8+10+12" does not work.. remembering that, 2+2+2+2+2+2.. = (2,4,6,8,10,12) is different from 2+4+6+8+10+12 = (2,6,12,20,30,42).

  • @William If all you need to do is add those who are equal in the two it’s a matter of putting the sum only within the if (comparacao == 0) {. If that is not the case, unfortunately your example has been far from what it needs and in this way you have just had an answer far from what you need. So I suggest you make it more clear what you’re really trying to do

  • @William And why didn’t you put in the formulas you really need? Make the question as clear as possible by including the formulas you are actually using and the expected result for these formulas.

  • I’m sorry I thought it would work... you can help me Isac?? =/

  • @William since my answer answers 100 to this question I advise you to finalize this question and open a new one with exactly the formulas you need to use and the results you have to give. I’ll answer that question if it’s clear what you want.

  • All right Isac, I finished and posted another question... https://answall.com/questions/276084/como-imprimir-apenas-n%C3%Bameros-que-se-repetem-entre-duas-formulas-progressivas-dif

Show 1 more comment

Browser other questions tagged

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