Im trying to create a program for the collatz conjecture, and I need it to work for bigger numbers. So, i tried to use the BigInteger class, but it seems to run into an error with the two types: bigint and int.
I've looked up the problems for big int, and it seemingly works with the addition and multiplication. However, when trying to get the remainder when dividing by two, it returns an error. (At the start of runtime).
import java.math.BigInteger;
import java.math.Bigdecimal;
import java.util.Objects;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
boolean repeat = true;
while (repeat){
int i = 0;
Scanner intIn = new Scanner(System.in);
Scanner strIn = new Scanner(System.in);
System.out.println("Enter number you want solved, Don't type 0. Use this program with caution");
BigInteger num = intIn.nextBigInteger();
while (num.equals(new BigInteger("1")) == false){
if (num.equals(new BigInteger("1"))){
// The issue currently is that the remainder function returns an error, but im not sure exactly what is wrong with it.
} else if (num.remainder(new BigInteger("2")) == 0) {
num = num.divide(new BigInteger("2"));
} else {
num = num.multiply(new BigInteger("3"));
num = num.add(new BigInteger("1"));
}
i += 1;
}
System.out.println("It took " + i + " steps to reach 1. Would you like to do another number? (yes/no)");
String yn = strIn.nextLine();
if (Objects.equals(yn, "no")){
repeat = false;
}
}
}
}
In line 21, the error is with the remainder function. (in case you missed the comment)