Getting TLE for below code


#1
import java.lang.*;

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

public class Main {

public static BigInteger sqrt(BigInteger x) {
BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
BigInteger div2 = div;
// Loop until we hit the same value twice in a row, or wind
// up alternating.
for(;:wink: {
BigInteger y = div.add(x.divide(div)).shiftRight(1);
if (y.equals(div) || y.equals(div2))
return y;
div2 = div;
div = y;
}
}

public static void main(String[] args) {
    // YOUR CODE GOES HERE
    // Please take input and print output to standard input/output (stdin/stdout)
    // DO NOT USE ARGUMENTS FOR INPUTS
    // E.g. 'Scanner' for input & 'System.out' for output
    Scanner sc = new Scanner(System.in);
    BigInteger num = sc.nextBigInteger();
    int flag=0;
    sc.close();
    // BigInteger div = num.divide(BigInteger.valueOf(2));
    BigInteger sqrt = sqrt(num);
    BigInteger start = new BigInteger("2");
    for(BigInteger i=start;i.compareTo(sqrt)<=0;i=i.add(BigInteger.ONE)) {
        if(num.mod(i).equals(BigInteger.ZERO)) {
            flag=1;break;
        }
    }
    if(flag==1) {
        System.out.println("not prime");
    }
    else {
        System.out.println("prime");
    }
}

}