Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to calculate the square root of BigInteger in Java

1 Answer

0 votes
package javaapplication1;
 
import java.math.BigDecimal;
import java.math.BigInteger;

public class JavaApplication1 {
 
    public static void main(String[] args) {
  
        BigInteger m2sl3000 = new BigInteger("0");
        BigInteger b = new BigInteger("2");

        m2sl3000 = b.shiftLeft(3000 - 1);

        String sn = m2sl3000 + "";

        BigDecimal d = new BigDecimal(sn);
        BigDecimal n1 = new BigDecimal("1");
        BigDecimal n2 = new BigDecimal("2");
        BigDecimal epsilon = new BigDecimal("0.0000000001");

        BigDecimal dByn1 = d.divide(n1, 9, BigDecimal.ROUND_FLOOR);

        BigDecimal dByn1addn1 = dByn1.add(n1);

        BigDecimal dByn1addn1byn2 = dByn1addn1.divide(n2, 9, BigDecimal.ROUND_FLOOR);

        BigDecimal sr = dByn1addn1byn2;
        int i = 99;

        do {
            n1 = dByn1addn1byn2;
            dByn1 = d.divide(n1, 9, BigDecimal.ROUND_FLOOR);
            dByn1addn1 = dByn1.add(n1);
            dByn1addn1byn2 = dByn1addn1.divide(n2, 9, BigDecimal.ROUND_FLOOR);
            BigDecimal savegdiff = sr.subtract(dByn1addn1byn2);

            if (savegdiff.compareTo(epsilon) == -1) {
                i = 0;
            } else {
                sr = dByn1addn1byn2;
            }

        } while (i > 1);

        System.out.println("BigInteger: " + sn + "\nLength: " + sn.length() + "\nSquare Root: " + sr);
    }
}
 
/*
    
run:
    
BigInteger: 1230231922161117176931558813276752514640713895736833715766118029160058800614672948775360067838593459582429649254051804908512884180898236823585082482065348331234959350355845017413023320111360666922624728239756880416434478315693675013413090757208690376793296658810662941824493488451726505303712916005346747908623702673480919353936813105736620402352744776903840477883651100322409301983488363802930540482487909763484098253940728685132044408863734754271212592471778643949486688511721051561970432780747454823776808464180697103083861812184348565522740195796682622205511845512080552010310050255801589349645928001133745474220715013683413907542779063759833876101354235184245096670042160720629411581502371248008430447184842098610320580417992206662247328722122088513643683907670360209162653670641130936997002170500675501374723998766005827579300723253474890612250135171889174899079911291512399773872178519018229989376
Length: 904
Square Root: 35074662110434038747627587960280857993524015880330828824075798024790963850563322203657080886584969261653150406795437517399294548941469959754171038918004700847889956485329097264486802711583462946536682184340138629451355458264946342525383619389314960644665052551751442335509249173361130355796109709885580674313954210217657847432626760733004753275317192133674703563372783297041993227052663333668509952000175053355529058880434182538386715523683713208549376.000000000
    
*/

 



answered Nov 2, 2016 by avibootz
...