So in my computer science class we have been learning how to code with java, and I've come across my first limitation within coding itself. The problem is that long only allows you to store up to 64 bits or a number close to that. So we've started doing prime detection with 16 digit numbers and seeing how long it takes the computers to do the computation, but I'm looking to go past this 16 bit limitation. So I've looked into BigIntegers and frankly I don't know how to use them. I understand you have to import them and make a variable equal to a BigInteger value but when I plug that variable into a for loop it comes up with this error(s):
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method sqrt(double) in the type Math is not applicable for the arguments (BigInteger)
The operator % is undefined for the argument type(s) BigInteger, long
at PrimeNumbers.main(PrimeNumbers.java:16)
Here is my code:
import java.math.BigInteger;
Scanner input = new Scanner(System.in);
BigInteger number;
number = new BigInteger("48112959837082048697");
System.out.println(number);
for(long x = 2; x < Math.sqrt(number); x++) {
if(number % x == 0) {
System.out.println("not a prime");
}
}
System.out.println("Prime");
All in all, I just want my for loop to check the number, if there's a workaround to using BigInteger that would be great too. BigInteger is just the first thing I found. As for looking it up elsewhere, a lot of the instructions online were too complicated or I just don't understand what they're saying just yet.
This is my first time asking a question so I don't know if it's too long and redundant and if so I'm sorry, but thanks in advance to anyone that answers my question.