square root Long - Android java.lang

Android examples for java.lang:Math

Description

square root Long

Demo Code


//package com.java2s;

public class Main {
    public static long sqrtLong(long n) {
        long low = 1;
        long high = n;
        long medium, square;
        do {/*  w w  w. j a v a2s .com*/
            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