How to apply "If" & "Else" functions to Biginteger?

Asked

Viewed 103 times

2

I have this code in Long functioning normally:

public static void main(String args[]){

  List<Long> lista = new ArrayList();  

    for(long a = 1; a <= 100; a++){

    if(a%2==0) {}
            else if (a%3==0){}
            else {
             lista.add(a);
               }   
         }
    System.out.println(lista);
   }
}

saída certa

I made this other code in BigInteger for the same function, but does not work:

public static void main(String[] args) {

    List<BigInteger> lista = new ArrayList();

    BigInteger começo = new BigInteger("1");
    BigInteger fim = new BigInteger("100");
    BigInteger n0 = new BigInteger("0");
    BigInteger n2 = new BigInteger("2");
    BigInteger n3 = new BigInteger("3");

for (BigInteger a = começo; a.compareTo(fim) <= 0; a = a.add(BigInteger.ONE)) {

   if (a.divide(n2)==n0){}
   else if (a.divide(n3)==n0){}
   else {
       lista.add(a);
      }
    } 
  System.out.println( lista );
  }
}

saída errada

1 answer

1


You are just using the wrong method. In your original code with long, you wore the % which is the rest of the division. Already in your code with BigInteger, you are using divide, that makes division and not rest of division. You should be using the method remainder instead.

There’s still another problem. You should use the equals instead of ==. The reason is the same why you should not compare Strings with ==, i.e., the == compares whether two objects are in the same memory position, and do not have equal content.

In addition, you can also use the method valueOf(long) which is much more practical than the builder who receives a String. The use of constants is also recommended BigInteger.ZERO, BigInteger.ONE and BigInteger.TEN.

Finally, there is no need for clauses ifs and else ifempty s, you can always rearrange the corresponding boolean expression.

Here’s your resulting code:

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

class Divisao {
    public static void main(String[] args) {
        List<BigInteger> lista = new ArrayList<>();

        BigInteger começo = BigInteger.ONE;
        BigInteger fim = BigInteger.valueOf(100);
        BigInteger n0 = BigInteger.ZERO;
        BigInteger n2 = BigInteger.valueOf(2);
        BigInteger n3 = BigInteger.valueOf(3);

        for (BigInteger a = começo; a.compareTo(fim) <= 0; a = a.add(BigInteger.ONE)) {
            if (!a.remainder(n2).equals(n0) && !a.remainder(n3).equals(n0)) {
                lista.add(a);
            }
        } 
        System.out.println(lista);
    }
}

Here’s his way out:

[1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35, 37, 41, 43, 47, 49, 53, 55, 59, 61, 65, 67, 71, 73, 77, 79, 83, 85, 89, 91, 95, 97]

See here working on ideone.

  • Thanks Buddy, how do I do the same instead of being Biginteger in Bigdecimal?

  • @Rafael Almost that. The difference is that the equals(Object) may not be enough, since in the BigDecimal, he considers that the values 123.456 and 123.4560 are different because the zeros on the left are considered. In this case you use the compareTo(BigDecimal) or the signum().

Browser other questions tagged

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