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

Asked

Viewed 68 times

0

I have two progressive formulas of 10 numbers.
Each will be added to a certain number.

The first formula progresses like this 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 case "1".
Final Result will be:(3,7,13,21,31,43,57,73,91,111).

The second formula progresses like this 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 case "3".
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: https://repl.it/repls/PeacefulOvalPhases

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.

Therefore the output of the program should print only:

7
43

Important to note: It cannot be in ArrayList, and the numbers need to be BigInteger.

1 answer

0


Using the same technique I used in your previous question can reach the desired result. The technique involves using two increment variables, one for each formula. And with these increase only the smallest and compare:

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 obtained is actually:

7
43

And not just the 7 as I indicated.

See code working on Ideone

  • Thank you very much Isac... It was just that... It worked this time...

Browser other questions tagged

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