is Prime Int - Android java.lang

Android examples for java.lang:Math

Description

is Prime Int

Demo Code


//package com.java2s;

public class Main {
    public static boolean isPrimeInt(long j) {
        long k = sqrtLong(j);
        if (j % 2 == 0)
            return false;
        for (long p = 3; p <= k; p += 2)
            if (j % p == 0)
                return false;
        return true;
    }/*from   w  w w . j  a  va2  s  .c  o  m*/

    public static long sqrtLong(long n) {
        long low = 1;
        long high = n;
        long medium, square;
        do {
            medium = (high + low) / 2;
            square = medium * medium;
            if (square < n)
                low = medium;
            if (square > n)
                high = medium;
            if (square == n)
                return medium;
        } while (high > low + 1);
        if (high * high == n)
            return high;
        else
            return low;
    }
}

Related Tutorials