0

I have a code for primality test that accepts integers up to 10 digits, but I want to extend this expansively so that the code accepts more than 200 digits. What should I switch in the code?

import java.util.*; 
import java.math.*; 

class CheckPrimeTest { 

    static boolean checkPrime(long n) 
    { 
        // Corner case 
        if (n <= 1) return false; 

        // Check from 2 to n-1 
        for (int i = 2; i < n; i++) 
            if (n % i == 0) 
                return false; 

        return true; 
    } 

    // Driver Program  
    public static void main(String args[]) 

                         throws java.lang.Exception 
    { 
 Scanner input = new Scanner(System.in);
  System.out.print("Enter an integer: ");
        long n = input.nextInt();  

       System.out.println(checkPrime(n)); 

    } 
} 
Ryuk
  • 37
  • 1
  • 4
  • 1
    Possible duplicate of [Large Numbers in Java](https://stackoverflow.com/questions/849813/large-numbers-in-java) – CloudPotato Oct 22 '19 at 18:17

1 Answers1

1

If you have such large numbers you can't use ints and longs, you will have to use BigInteger which already defines an isProbablePrime method for you:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    BigInteger integer = new BigInteger(scanner.nextLine());
    System.out.println(integer.isProbablePrime(1));
}
Alex R
  • 3,139
  • 1
  • 18
  • 28