How to print only numbers that repeat between two different progressive formulas? [duplicate]

0

I have two progressive formulas of 10 numbers. Each of them will add up to a certain number.

The first formula thus progresses 0+2+4+6+8+10+12+14+16+18+20 . And its result is ( 2,6,12,20,30,42,56,72,90,110 ). Adding this result to an established number, in the "1" case.
Final result will be: ( 3,7,13,21,31,43,57,73,91,111 ).

The second formula thus progresses 0+4+8+12+16+20+24+28+32+36+40 . And its result is ( 4,12,24,40,60,84,112,144,180,220 ). Adding this result to an established number, in the "3" case.
Final result will be: ( 7,15,27,43,63,87,115,147,183,223 ).

Code:

public static void main(String[] args) {

        BigInteger start = new BigInteger("1");
        BigInteger limit = new BigInteger("10");
        BigInteger zap1 = new BigInteger("0");
        BigInteger zap2 = new BigInteger("0");
        BigInteger n2 = new BigInteger("2"); //formula(1) progressiva
        BigInteger n4 = new BigInteger("4"); //formula(2) progressiva
        BigInteger soma1 = new BigInteger("1"); //número(1) estabelecido     
        BigInteger soma2 = new BigInteger("3"); //número(2) estabelecido

for (BigInteger a = start; a.compareTo(limit) <= 0; a = a.add(BigInteger.ONE)) {

zap1 = zap1.add(n2.multiply(a));  //formula(1) progressiva
zap2 = zap2.add(n4.multiply(a));  //formula(2) progressiva          

BigInteger copas1 = zap1.add(soma1);  //Resultado final(1)
BigInteger copas2 = zap2.add(soma2);  //Resultado final(2)

        System.out.println( copas1 );
        System.out.println( copas2 );       

          }
      }

See working on repl: link

Final result of the first formula: ( 3,7,13,21,31,43,57,73,91,111 ). Final result of the second formula: ( 7,15,27,43,63,87,115,147,183,223 ).

Comparing the final results of the two formulas we see that the repeated numbers are:
The 7 and 43 .

So the output of the program should only print:

7
43

Important to note that: It can not be in ArrayList , and the numbers must be BigInteger .

    
asked by anonymous 13.02.2018 / 18:41

1 answer

0

Using the same technique I used in your previous question can achieve the desired result. The technique uses two increment variables, one for each formula. And with these increment just the smallest and go comparing:

BigInteger start = new BigInteger("1");
BigInteger limit = new BigInteger("10");
BigInteger zap1 = new BigInteger("0");
BigInteger zap2 = new BigInteger("0");
BigInteger n2 = new BigInteger("2"); // formula(1) progressiva
BigInteger n4 = new BigInteger("4"); // formula(2) progressiva
BigInteger soma1 = new BigInteger("1"); // número(1) estabelecido
BigInteger soma2 = new BigInteger("3"); // número(2) estabelecido

for (BigInteger a = start, b=start; a.compareTo(limit) <= 0 && b.compareTo(limit) <= 0;) {

    //comparação agora com a logica de ambas as formulas
    int comparacao = zap1.add(soma1).compareTo(zap2.add(soma2));

    if (comparacao == 0 && zap1.compareTo(BigInteger.ZERO) != 0 && zap2.compareTo(BigInteger.ZERO) != 0) {
        System.out.println(zap1.add(soma1));
    }

    //aumenta apenas o mais pequeno
    if (comparacao <= 0) {      
        zap1 = zap1.add(n2.multiply(a)); // formula(1) progressiva
        a = a.add(BigInteger.ONE);
    }
    else {      
        zap2 = zap2.add(n4.multiply(b)); // formula(2) progressiva
        b = b.add(BigInteger.ONE);
    }
}

Note that the result is actually:

7
43

And not just the 7 as indicated.

See the code working on Ideone

    
13.02.2018 / 22:13