1

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.

  • 16 bits = 2^16. 16 digits implies 10^16. These are not even remotely close to each other's scale. – Makoto Oct 02 '18 at 20:11
  • My bad, meant 64 bits. – Valentino Rozzi Oct 02 '18 at 20:13
  • 1
    It's worth taking a look at what you're actually doing here mathematically. Even if this compiled, it'll be way too slow to run on any integer that is large enough to need `BigInteger`. – Mysticial Oct 02 '18 at 20:13
  • 3
    You have to use `BigInteger#someMethod(`... [Here is everything about the class BigInteger](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html) – zlakad Oct 02 '18 at 20:15
  • Well that's kind of the point. We want long computations, hence why I wanted to make it larger, to slow it down to the point that it could take, maybe, half a day to process. – Valentino Rozzi Oct 02 '18 at 20:16
  • @ValentinoRozzi What's the ultimate goal? If it's just to simulate slow, why bother wasting CPU time? – Dave Newton Oct 02 '18 at 20:19
  • Java doesn't have operator overloads, so operators like % won't work with classes and you have to replace it with the appropriate BigInteger method. Also functions such as Math.sqrt is not defined to take BigInteger as an argument. – matt Oct 02 '18 at 20:20
  • @DaveNewton Essentially, yes. Why waste my time, I don't know but as for now it give me an excuse to try and learn how to use different things outside the realm in which I've already been taught. – Valentino Rozzi Oct 02 '18 at 20:21
  • @matt Ok, I see. So I definitely wouldn't be able to use % and Math.sqrt . Would I still be able to use the for loop though or would I have to scrap everything completely? – Valentino Rozzi Oct 02 '18 at 20:22
  • You have to use the appropriate method, eg for square root. https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html#sqrt() or you could write `BigInteger r = number.sqrt()` I think you can guess the method for mod. – matt Oct 02 '18 at 20:24
  • Reference the following: https://docs.oracle.com/javase/10/docs/api/java/math/BigInteger.html – Rex Charles Oct 02 '18 at 20:24
  • @matt Looking at that now, and thanks. – Valentino Rozzi Oct 02 '18 at 20:39

1 Answers1

3

To use BigInteger correctly, you need to its methods https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html.

So in your case.

    for (BigInteger x = new BigInteger("2"); x.pow(2).compareTo(number) < 0; x = x.add(BigInteger.ONE)) {
        if (number.mod(x).compareTo(BigInteger.ZERO) == 0) {
            System.out.println("not a prime dividable by" + x); 
        }
    }
k5_
  • 5,450
  • 2
  • 19
  • 27
  • Ahh ok, this makes a lot more sense. I will fiddle around with it but this, like I said, makes a lot more sense. Thank you. – Valentino Rozzi Oct 02 '18 at 20:28
  • 1
    To add to that (as @ValentinoRozzi seems to just be starting): int, long are (among others) Java's primitive numeric data types, hence you can use operators like `%` with them. BigInteger is a complex class, that allows you to do math with large numbers, but cant use the primitives operands (Java has no operator overloading). Therefore it provides it's own mathematical functions like e.g. `BigInteger largeMod = largeNumber.mod(anotherLargeNumber);` Happy Coding – deR_Ed Oct 02 '18 at 20:31
  • @deR_Ed Ok, so I want to draw analogy of some sorts. So due to the fact that I am importing it, that means that its not a part of traditional java so you can't use, as you said, traditional operands? – Valentino Rozzi Oct 02 '18 at 20:38
  • 1
    @ValentinoRozzi unfortunately, the rabbit hole goes way deeper (just look here, for example: https://stackoverflow.com/questions/5199359/why-do-people-still-use-primitive-types-in-java). But for now - yes, if it needs to be imported, you can't use "traditional" operators. I strongly suggest to read some material on the topic (it also will probably be part of your cs class some time soon) – deR_Ed Oct 02 '18 at 20:49
  • 1
    @deR_Ed Oh my, yeah I’m gonna have to do some research for sure. I’m sure my teacher will talk about primitives but I doubt it will go that in depth as it’s just a high school class (even though AP). But this adds an element to coding that I never knew existed and definitely opens it up to many more possibilities than I originally considered. Thanks, for all the help. – Valentino Rozzi Oct 02 '18 at 20:55
  • @ValentinoRozzi Primitives are not Object-Oriented Programming. They were included in Java because at that time any new language that did not look like C would fail to catch on with the mainstream. And primitives also made it much easier to port C code to Java. You can think of Java as a hybrid of OOP and non-OOP. Now with auto-boxing, the line between the two has blurred. – Basil Bourque Oct 03 '18 at 07:02