Software & Finance





C# - Find SQRT using Algorithm





You can solve the problem in two ways with out using the built in function System.Math.Sqrt.

 

Method 1 - Like binary search, have a minimum and maximum possible values. Do the square operation and compare the result. Then adjust minimum or maximum until we find the correct sqrt of the given number. NOTE: This is NOT a perfect square root and it has got accuracy of 4 decimal points. To access the C# source code for method1, click here.

 

Method 2 - The traditional way of doing with out calculator or any assumption is using the algorithm. It will be perfect square root up to N number of decimal points meaning the limitation of float and double. Method 2 is explained on this page.

 

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

 

namespace SoftwareAndFinance

{

    class Math

    {

        public static double SqrtByAlogorithm(double x)

        {

            long numeric = (long) x;

            long n = numeric;

            long fraction =(long) ((x - numeric) * 1000000); // 6 digits

            long f = fraction;

            int numdigits = 0, fnumdigits = 0, currdigits = 0;

            int tempresult = 0;

            int bOdd = 0, part = 0, tens = 1;

            int fractioncount = 0;

            double result = 0;

            int k, f1, f2, i, num, temp, quotient;

           

            for(numdigits = 0; n >= 10; numdigits++)

                n = (n / 10);

            numdigits++;

           

            for(fnumdigits = 0; f >= 10; fnumdigits++)

                f = (f / 10);

            fnumdigits++;

 

            if( (numdigits % 2) == 1)

                bOdd = 1;

 

            while(true)

            {

                tens = 1;

                currdigits = (bOdd == 1) ? (numdigits - 1) : (numdigits - 2);

 

                for(k = 0; k < currdigits; k++)

                    tens *= 10;

                part = (int) numeric / tens;

               

                // Get the Nearest Multiplication Factor

                num = part;

                quotient = tempresult * 2;

                i = 0;

                temp = 0;

                for(i = 1; ;i++)

                {

                    if(quotient == 0)

                    {

                        if(num - i * i < 0)

                        {

                             tempresult = (i - 1);

                             break;

                        }

                    }

                    else

                    {

                        temp = quotient * 10 + i;

                        if(num - i * temp < 0)

                        {

                            tempresult = quotient / 2 * 10 + i - 1;

                            break;

                        }

                    }

                }

                // Done with Nearest Multiplication Factor

                f1 = tempresult / 10;

                f2 = tempresult % 10;

 

                if(f1 == 0)

                    numeric = numeric - (tempresult * tempresult * tens);

                else

                    numeric = numeric - ((f1 * 2 * 10 + f2) * f2 * tens);

 

                if(numeric == 0 && fraction == 0)

                {

                    if(currdigits > 0)

                    {

                        // Handle the Zero case

                        tens = 1;

                        currdigits = currdigits / 2;

                        for(k = 0; k < currdigits; k++)

                            tens *= 10;

                        tempresult *= tens;

                    }

                    break;

                }

 

                if(bOdd == 1)

                {

                    numdigits -= 1;

                    bOdd = 0;

                }

                else

                    numdigits -= 2;

 

                if( numdigits <= 0)

                {

                    if(numeric > 0 || fraction > 0)

                    {

                        if(fractioncount >= 5)

                            break;

                        // Handle the fraction part for integer numbers

                        fractioncount++;

                        numeric *= 100;

                        if(fraction > 0)

                        {

                            // Handle the fraction part for real numbers

                            fnumdigits -= 2;

                            tens = 1;

                            for(k = 0; k < fnumdigits; k++)

                                tens *= 10;

                            numeric += fraction / tens;

                            fraction = fraction % tens;

                        }

                        numdigits += 2;

                    }

                    else

                        break;

                }

            }

 

            if(fractioncount == 0)

                result = tempresult;

            else

            {

                tens = 1;

                for(k = 0; k < fractioncount; k++)

                    tens *= 10;

                result = (double) tempresult / tens;

            }

            return result;

        }

 

 

 

        static void Main(string[] args)

        {

            for (double d = 0; d <= 10000; d += 50)

                Console.WriteLine("sqrt({0,6:f1}) = {1,8:f4} {2,8:f4}", d, SqrtByAlogorithm(d), System.Math.Sqrt(d));           

        }

    }

}

Click here to get the C# project along with executable

 

Output


sqrt(   0.0) =   0.0000   0.0000

sqrt(  50.0) =   7.0711   7.0711

sqrt( 100.0) =  10.0000  10.0000

sqrt( 150.0) =  12.2474  12.2474

sqrt( 200.0) =  14.1421  14.1421

sqrt( 250.0) =  15.8114  15.8114

sqrt( 300.0) =  17.3205  17.3205

sqrt( 350.0) =  18.7083  18.7083

sqrt( 400.0) =  20.0000  20.0000

sqrt( 450.0) =  21.2132  21.2132

sqrt( 500.0) =  22.3607  22.3607

sqrt( 550.0) =  23.4521  23.4521

sqrt( 600.0) =  24.4949  24.4949

sqrt( 650.0) =  25.4951  25.4951

sqrt( 700.0) =  26.4575  26.4575

sqrt( 750.0) =  27.3861  27.3861

sqrt( 800.0) =  28.2843  28.2843

sqrt( 850.0) =  29.1548  29.1548

sqrt( 900.0) =  30.0000  30.0000

sqrt( 950.0) =  30.8221  30.8221

sqrt(1000.0) =  31.6228  31.6228

sqrt(1050.0) =  32.4037  32.4037

sqrt(1100.0) =  33.1662  33.1662

sqrt(1150.0) =  33.9116  33.9116

sqrt(1200.0) =  34.6410  34.6410

sqrt(1250.0) =  35.3553  35.3553

sqrt(1300.0) =  36.0555  36.0555

sqrt(1350.0) =  36.7423  36.7423

sqrt(1400.0) =  37.4166  37.4166

sqrt(1450.0) =  38.0789  38.0789

sqrt(1500.0) =  38.7298  38.7298

sqrt(1550.0) =  39.3700  39.3700

sqrt(1600.0) =  40.0000  40.0000

sqrt(1650.0) =  40.6202  40.6202

sqrt(1700.0) =  41.2311  41.2311

sqrt(1750.0) =  41.8330  41.8330

sqrt(1800.0) =  42.4264  42.4264

sqrt(1850.0) =  43.0116  43.0116

sqrt(1900.0) =  43.5890  43.5890

sqrt(1950.0) =  44.1588  44.1588

sqrt(2000.0) =  44.7214  44.7214

sqrt(2050.0) =  45.2769  45.2769

sqrt(2100.0) =  45.8258  45.8258

sqrt(2150.0) =  46.3681  46.3681

sqrt(2200.0) =  46.9042  46.9042

sqrt(2250.0) =  47.4342  47.4342

sqrt(2300.0) =  47.9583  47.9583

sqrt(2350.0) =  48.4768  48.4768

sqrt(2400.0) =  48.9898  48.9898

sqrt(2450.0) =  49.4975  49.4975

sqrt(2500.0) =  50.0000  50.0000

sqrt(2550.0) =  50.4975  50.4975

sqrt(2600.0) =  50.9902  50.9902

sqrt(2650.0) =  51.4782  51.4782

sqrt(2700.0) =  51.9615  51.9615

sqrt(2750.0) =  52.4404  52.4404

sqrt(2800.0) =  52.9150  52.9150

sqrt(2850.0) =  53.3854  53.3854

sqrt(2900.0) =  53.8516  53.8516

sqrt(2950.0) =  54.3139  54.3139

sqrt(3000.0) =  54.7723  54.7723

sqrt(3050.0) =  55.2268  55.2268

sqrt(3100.0) =  55.6776  55.6776

sqrt(3150.0) =  56.1249  56.1249

sqrt(3200.0) =  56.5685  56.5685

sqrt(3250.0) =  57.0088  57.0088

sqrt(3300.0) =  57.4456  57.4456

sqrt(3350.0) =  57.8792  57.8792

sqrt(3400.0) =  58.3095  58.3095

sqrt(3450.0) =  58.7367  58.7367

sqrt(3500.0) =  59.1608  59.1608

sqrt(3550.0) =  59.5819  59.5819

sqrt(3600.0) =  60.0000  60.0000

sqrt(3650.0) =  60.4152  60.4152

sqrt(3700.0) =  60.8276  60.8276

sqrt(3750.0) =  61.2372  61.2372

sqrt(3800.0) =  61.6441  61.6441

sqrt(3850.0) =  62.0484  62.0484

sqrt(3900.0) =  62.4500  62.4500

sqrt(3950.0) =  62.8490  62.8490

sqrt(4000.0) =  63.2456  63.2456

sqrt(4050.0) =  63.6396  63.6396

sqrt(4100.0) =  64.0312  64.0312

sqrt(4150.0) =  64.4205  64.4205

sqrt(4200.0) =  64.8074  64.8074

sqrt(4250.0) =  65.1920  65.1920

sqrt(4300.0) =  65.5744  65.5744

sqrt(4350.0) =  65.9545  65.9545

sqrt(4400.0) =  66.3325  66.3325

sqrt(4450.0) =  66.7083  66.7083

sqrt(4500.0) =  67.0820  67.0820

sqrt(4550.0) =  67.4537  67.4537

sqrt(4600.0) =  67.8233  67.8233

sqrt(4650.0) =  68.1909  68.1909

sqrt(4700.0) =  68.5565  68.5565

sqrt(4750.0) =  68.9202  68.9202

sqrt(4800.0) =  69.2820  69.2820

sqrt(4850.0) =  69.6419  69.6419

sqrt(4900.0) =  70.0000  70.0000

sqrt(4950.0) =  70.3562  70.3562

sqrt(5000.0) =  70.7107  70.7107

sqrt(5050.0) =  71.0634  71.0634

sqrt(5100.0) =  71.4143  71.4143

sqrt(5150.0) =  71.7635  71.7635

sqrt(5200.0) =  72.1110  72.1110

sqrt(5250.0) =  72.4569  72.4569

sqrt(5300.0) =  72.8011  72.8011

sqrt(5350.0) =  73.1437  73.1437

sqrt(5400.0) =  73.4847  73.4847

sqrt(5450.0) =  73.8241  73.8241

sqrt(5500.0) =  74.1620  74.1620

sqrt(5550.0) =  74.4983  74.4983

sqrt(5600.0) =  74.8331  74.8331

sqrt(5650.0) =  75.1665  75.1665

sqrt(5700.0) =  75.4983  75.4983

sqrt(5750.0) =  75.8288  75.8288

sqrt(5800.0) =  76.1577  76.1577

sqrt(5850.0) =  76.4853  76.4853

sqrt(5900.0) =  76.8115  76.8115

sqrt(5950.0) =  77.1362  77.1362

sqrt(6000.0) =  77.4597  77.4597

sqrt(6050.0) =  77.7817  77.7817

sqrt(6100.0) =  78.1025  78.1025

sqrt(6150.0) =  78.4219  78.4219

sqrt(6200.0) =  78.7401  78.7401

sqrt(6250.0) =  79.0569  79.0569

sqrt(6300.0) =  79.3725  79.3725

sqrt(6350.0) =  79.6869  79.6869

sqrt(6400.0) =  80.0000  80.0000

sqrt(6450.0) =  80.3119  80.3119

sqrt(6500.0) =  80.6226  80.6226

sqrt(6550.0) =  80.9321  80.9321

sqrt(6600.0) =  81.2404  81.2404

sqrt(6650.0) =  81.5475  81.5475

sqrt(6700.0) =  81.8535  81.8535

sqrt(6750.0) =  82.1584  82.1584

sqrt(6800.0) =  82.4621  82.4621

sqrt(6850.0) =  82.7647  82.7647

sqrt(6900.0) =  83.0662  83.0662

sqrt(6950.0) =  83.3667  83.3667

sqrt(7000.0) =  83.6660  83.6660

sqrt(7050.0) =  83.9643  83.9643

sqrt(7100.0) =  84.2615  84.2615

sqrt(7150.0) =  84.5577  84.5577

sqrt(7200.0) =  84.8528  84.8528

sqrt(7250.0) =  85.1469  85.1469

sqrt(7300.0) =  85.4400  85.4400

sqrt(7350.0) =  85.7321  85.7321

sqrt(7400.0) =  86.0233  86.0233

sqrt(7450.0) =  86.3134  86.3134

sqrt(7500.0) =  86.6025  86.6025

sqrt(7550.0) =  86.8907  86.8907

sqrt(7600.0) =  87.1780  87.1780

sqrt(7650.0) =  87.4643  87.4643

sqrt(7700.0) =  87.7496  87.7496

sqrt(7750.0) =  88.0341  88.0341

sqrt(7800.0) =  88.3176  88.3176

sqrt(7850.0) =  88.6002  88.6002

sqrt(7900.0) =  88.8819  88.8819

sqrt(7950.0) =  89.1628  89.1628

sqrt(8000.0) =  89.4427  89.4427

sqrt(8050.0) =  89.7218  89.7218

sqrt(8100.0) =  90.0000  90.0000

sqrt(8150.0) =  90.2774  90.2774

sqrt(8200.0) =  90.5539  90.5539

sqrt(8250.0) =  90.8295  90.8295

sqrt(8300.0) =  91.1043  91.1043

sqrt(8350.0) =  91.3783  91.3783

sqrt(8400.0) =  91.6515  91.6515

sqrt(8450.0) =  91.9239  91.9239

sqrt(8500.0) =  92.1954  92.1954

sqrt(8550.0) =  92.4662  92.4662

sqrt(8600.0) =  92.7362  92.7362

sqrt(8650.0) =  93.0054  93.0054

sqrt(8700.0) =  93.2738  93.2738

sqrt(8750.0) =  93.5414  93.5414

sqrt(8800.0) =  93.8083  93.8083

sqrt(8850.0) =  94.0744  94.0744

sqrt(8900.0) =  94.3398  94.3398

sqrt(8950.0) =  94.6044  94.6044

sqrt(9000.0) =  94.8683  94.8683

sqrt(9050.0) =  95.1315  95.1315

sqrt(9100.0) =  95.3939  95.3939

sqrt(9150.0) =  95.6556  95.6556

sqrt(9200.0) =  95.9166  95.9166

sqrt(9250.0) =  96.1769  96.1769

sqrt(9300.0) =  96.4365  96.4365

sqrt(9350.0) =  96.6954  96.6954

sqrt(9400.0) =  96.9536  96.9536

sqrt(9450.0) =  97.2111  97.2111

sqrt(9500.0) =  97.4679  97.4679

sqrt(9550.0) =  97.7241  97.7241

sqrt(9600.0) =  97.9796  97.9796

sqrt(9650.0) =  98.2344  98.2344

sqrt(9700.0) =  98.4886  98.4886

sqrt(9750.0) =  98.7421  98.7421

sqrt(9800.0) =  98.9949  98.9949

sqrt(9850.0) =  99.2472  99.2472

sqrt(9900.0) =  99.4987  99.4987

sqrt(9950.0) =  99.7497  99.7497

sqrt(10000.0) = 100.0000 100.0000