Here you can find the source of sqrt(long val)
public static long sqrt(long val)
//package com.java2s; /*//from w w w . j av a2 s . c o m * Copyright 2016 Jerom van der Sar. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { public static long sqrt(long val) { // relative error tolerance double epsilon = 1e-15; // estimate of the square root double estimate = val; // repeatedly apply Newton update step until desired precision is achieved while (Math.abs(estimate - val / estimate) > epsilon * estimate) { estimate = (val / estimate + estimate) / 2l; } return (long) estimate; } }