Java - Find SQRT by Guess - Approximation method
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. Method 1 is explained on this page.
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. Click here to go to algorithmic source code for finding square root.
Source Code
import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.*;
class SqrtApproximation
{
static public double SQRTByGuess(double num)
{
// Assume that the number is less than 1,000,000. so that the maximum of SQRT would be 1000.
// Lets assume the result is 1000. If you want you can increase this limit
double min = 0, max = 1000;
double answer = 0;
double test = 0;
if (num < 0)
{
System.out.println("Negative numbers are not allowed");
return -1;
}
else if (num == 0)
return 0;
while (true)
{
test = (min + max) / 2;
answer = test * test;
if (num > answer)
{
// min needs be moved
min = test;
}
else if (num < answer)
{
// max needs be moved
max = test;
}
if (num == answer)
break;
if (num > (answer - 0.0001) &&
num < (answer + 0.0001))
break;
}
DecimalFormat fmt = new DecimalFormat("#####.#####");
return Double.valueOf(fmt.format(test));
}
public static void main(String[] args)
{
for (double d = 0; d <= 10000; d += 50)
{
System.out.print("sqrt(");
System.out.print(d);
System.out.print(") = ");
System.out.print(SQRTByGuess(d));
System.out.print(", ");
System.out.println(Math.sqrt(d));
}
}
}
Output
D:\Program Files\Java\jdk1.6.0_23\bin>javac SqrtApproximation.java
D:\Program Files\Java\jdk1.6.0_23\bin>java SqrtApproximation
sqrt(0.0) = 0.0, 0.0
sqrt(50.0) = 7.07106, 7.0710678118654755
sqrt(100.0) = 10.0, 10.0
sqrt(150.0) = 12.24745, 12.24744871391589
sqrt(200.0) = 14.14213, 14.142135623730951
sqrt(250.0) = 15.81139, 15.811388300841896
sqrt(300.0) = 17.32051, 17.320508075688775
sqrt(350.0) = 18.70829, 18.708286933869708
sqrt(400.0) = 20.0, 20.0
sqrt(450.0) = 21.2132, 21.213203435596427
sqrt(500.0) = 22.36068, 22.360679774997898
sqrt(550.0) = 23.45208, 23.45207879911715
sqrt(600.0) = 24.4949, 24.49489742783178
sqrt(650.0) = 25.4951, 25.495097567963924
sqrt(700.0) = 26.45751, 26.457513110645905
sqrt(750.0) = 27.38613, 27.386127875258307
sqrt(800.0) = 28.28427, 28.284271247461902
sqrt(850.0) = 29.15476, 29.154759474226502
sqrt(900.0) = 30.0, 30.0
sqrt(950.0) = 30.82207, 30.822070014844883
sqrt(1000.0) = 31.62278, 31.622776601683793
sqrt(1050.0) = 32.4037, 32.4037034920393
sqrt(1100.0) = 33.16625, 33.166247903554
sqrt(1150.0) = 33.91165, 33.91164991562634
sqrt(1200.0) = 34.64102, 34.64101615137755
sqrt(1250.0) = 35.35534, 35.35533905932738
sqrt(1300.0) = 36.05551, 36.05551275463989
sqrt(1350.0) = 36.74235, 36.742346141747674
sqrt(1400.0) = 37.41657, 37.416573867739416
sqrt(1450.0) = 38.07887, 38.07886552931954
sqrt(1500.0) = 38.72983, 38.72983346207417
sqrt(1550.0) = 39.37004, 39.370039370059054
sqrt(1600.0) = 40.0, 40.0
sqrt(1650.0) = 40.62019, 40.620192023179804
sqrt(1700.0) = 41.23106, 41.23105625617661
sqrt(1750.0) = 41.833, 41.83300132670378
sqrt(1800.0) = 42.42641, 42.42640687119285
sqrt(1850.0) = 43.01163, 43.01162633521314
sqrt(1900.0) = 43.58899, 43.58898943540674
sqrt(1950.0) = 44.15881, 44.15880433163923
sqrt(2000.0) = 44.72136, 44.721359549995796
sqrt(2050.0) = 45.27692, 45.27692569068709
sqrt(2100.0) = 45.82576, 45.8257569495584
sqrt(2150.0) = 46.36809, 46.36809247747852
sqrt(2200.0) = 46.90416, 46.9041575982343
sqrt(2250.0) = 47.43416, 47.43416490252569
sqrt(2300.0) = 47.95831, 47.958315233127195
sqrt(2350.0) = 48.4768, 48.47679857416329
sqrt(2400.0) = 48.9898, 48.98979485566356
sqrt(2450.0) = 49.49747, 49.49747468305833
sqrt(2500.0) = 50.0, 50.0
sqrt(2550.0) = 50.49752, 50.49752469181039
sqrt(2600.0) = 50.9902, 50.99019513592785
sqrt(2650.0) = 51.47815, 51.478150704935004
sqrt(2700.0) = 51.96152, 51.96152422706632
sqrt(2750.0) = 52.44044, 52.44044240850758
sqrt(2800.0) = 52.91503, 52.91502622129181
sqrt(2850.0) = 53.38539, 53.38539126015655
sqrt(2900.0) = 53.85165, 53.85164807134504
sqrt(2950.0) = 54.3139, 54.31390245600108
sqrt(3000.0) = 54.77226, 54.772255750516614
sqrt(3050.0) = 55.2268, 55.226805085936306
sqrt(3100.0) = 55.67764, 55.67764362830022
sqrt(3150.0) = 56.12486, 56.124860801609124
sqrt(3200.0) = 56.56854, 56.568542494923804
sqrt(3250.0) = 57.00877, 57.0087712549569
sqrt(3300.0) = 57.44563, 57.445626465380286
sqrt(3350.0) = 57.87919, 57.879184513951124
sqrt(3400.0) = 58.30952, 58.309518948453004
sqrt(3450.0) = 58.7367, 58.73670062235365
sqrt(3500.0) = 59.1608, 59.16079783099616
sqrt(3550.0) = 59.58188, 59.58187643906493
sqrt(3600.0) = 60.0, 60.0
sqrt(3650.0) = 60.41523, 60.41522986797286
sqrt(3700.0) = 60.82763, 60.8276253029822
sqrt(3750.0) = 61.23724, 61.237243569579455
sqrt(3800.0) = 61.64414, 61.644140029689765
sqrt(3850.0) = 62.04837, 62.048368229954285
sqrt(3900.0) = 62.44998, 62.44997998398398
sqrt(3950.0) = 62.84903, 62.849025449882674
sqrt(4000.0) = 63.24555, 63.245553203367585
sqrt(4050.0) = 63.63961, 63.63961030678928
sqrt(4100.0) = 64.03124, 64.03124237432849
sqrt(4150.0) = 64.42049, 64.42049363362563
sqrt(4200.0) = 64.80741, 64.8074069840786
sqrt(4250.0) = 65.19202, 65.19202405202648
sqrt(4300.0) = 65.57439, 65.57438524302
sqrt(4350.0) = 65.95453, 65.95452979136459
sqrt(4400.0) = 66.3325, 66.332495807108
sqrt(4450.0) = 66.70832, 66.70832032063167
sqrt(4500.0) = 67.08204, 67.08203932499369
sqrt(4550.0) = 67.45369, 67.45368781616021
sqrt(4600.0) = 67.8233, 67.82329983125268
sqrt(4650.0) = 68.19091, 68.19090848492928
sqrt(4700.0) = 68.55655, 68.55654600401044
sqrt(4750.0) = 68.92024, 68.92024376045111
sqrt(4800.0) = 69.28203, 69.2820323027551
sqrt(4850.0) = 69.64194, 69.6419413859206
sqrt(4900.0) = 70.0, 70.0
sqrt(4950.0) = 70.35624, 70.35623639735144
sqrt(5000.0) = 70.71068, 70.71067811865476
sqrt(5050.0) = 71.06335, 71.06335201775947
sqrt(5100.0) = 71.41428, 71.4142842854285
sqrt(5150.0) = 71.7635, 71.76350047203663
sqrt(5200.0) = 72.11103, 72.11102550927978
sqrt(5250.0) = 72.45688, 72.4568837309472
sqrt(5300.0) = 72.8011, 72.80109889280519
sqrt(5350.0) = 73.14369, 73.14369419163897
sqrt(5400.0) = 73.48469, 73.48469228349535
sqrt(5450.0) = 73.82412, 73.824115301167
sqrt(5500.0) = 74.16198, 74.16198487095663
sqrt(5550.0) = 74.49832, 74.4983221287567
sqrt(5600.0) = 74.83315, 74.83314773547883
sqrt(5650.0) = 75.16648, 75.16648189186454
sqrt(5700.0) = 75.49834, 75.49834435270749
sqrt(5750.0) = 75.82875, 75.82875444051551
sqrt(5800.0) = 76.15773, 76.15773105863909
sqrt(5850.0) = 76.48529, 76.48529270389177
sqrt(5900.0) = 76.81146, 76.81145747868608
sqrt(5950.0) = 77.13624, 77.13624310270757
sqrt(6000.0) = 77.45967, 77.45966692414834
sqrt(6050.0) = 77.78175, 77.78174593052023
sqrt(6100.0) = 78.1025, 78.10249675906654
sqrt(6150.0) = 78.42194, 78.4219357067906
sqrt(6200.0) = 78.74008, 78.74007874011811
sqrt(6250.0) = 79.05694, 79.05694150420949
sqrt(6300.0) = 79.37254, 79.37253933193772
sqrt(6350.0) = 79.68689, 79.68688725254614
sqrt(6400.0) = 80.0, 80.0
sqrt(6450.0) = 80.31189, 80.31189202104505
sqrt(6500.0) = 80.62258, 80.62257748298549
sqrt(6550.0) = 80.93207, 80.93207028119323
sqrt(6600.0) = 81.24038, 81.24038404635961
sqrt(6650.0) = 81.54753, 81.54753215150045
sqrt(6700.0) = 81.85353, 81.8535277187245
sqrt(6750.0) = 82.15838, 82.15838362577492
sqrt(6800.0) = 82.46211, 82.46211251235322
sqrt(6850.0) = 82.76473, 82.76472678623425
sqrt(6900.0) = 83.06624, 83.06623862918075
sqrt(6950.0) = 83.36666, 83.36666000266533
sqrt(7000.0) = 83.666, 83.66600265340756
sqrt(7050.0) = 83.96428, 83.96427811873333
sqrt(7100.0) = 84.2615, 84.26149773176358
sqrt(7150.0) = 84.55767, 84.55767262643882
sqrt(7200.0) = 84.85281, 84.8528137423857
sqrt(7250.0) = 85.14693, 85.14693182963201
sqrt(7300.0) = 85.44004, 85.44003745317531
sqrt(7350.0) = 85.73214, 85.73214099741124
sqrt(7400.0) = 86.02325, 86.02325267042627
sqrt(7450.0) = 86.31338, 86.31338250816034
sqrt(7500.0) = 86.60254, 86.60254037844386
sqrt(7550.0) = 86.89074, 86.89073598491383
sqrt(7600.0) = 87.17798, 87.17797887081348
sqrt(7650.0) = 87.46428, 87.46427842267951
sqrt(7700.0) = 87.74964, 87.74964387392122
sqrt(7750.0) = 88.03408, 88.03408430829505
sqrt(7800.0) = 88.31761, 88.31760866327846
sqrt(7850.0) = 88.60023, 88.60022573334675
sqrt(7900.0) = 88.88194, 88.88194417315589
sqrt(7950.0) = 89.16277, 89.16277250063504
sqrt(8000.0) = 89.44272, 89.44271909999159
sqrt(8050.0) = 89.72179, 89.7217922246318
sqrt(8100.0) = 90.0, 90.0
sqrt(8150.0) = 90.27735, 90.27735042633894
sqrt(8200.0) = 90.55385, 90.55385138137417
sqrt(8250.0) = 90.82951, 90.82951062292474
sqrt(8300.0) = 91.10434, 91.10433579144299
sqrt(8350.0) = 91.37833, 91.37833441248533
sqrt(8400.0) = 91.65151, 91.6515138991168
sqrt(8450.0) = 91.92388, 91.92388155425118
sqrt(8500.0) = 92.19544, 92.19544457292888
sqrt(8550.0) = 92.46621, 92.46621004453465
sqrt(8600.0) = 92.73618, 92.73618495495704
sqrt(8650.0) = 93.00538, 93.00537618869137
sqrt(8700.0) = 93.27379, 93.27379053088815
sqrt(8750.0) = 93.54143, 93.54143466934853
sqrt(8800.0) = 93.80832, 93.8083151964686
sqrt(8850.0) = 94.07444, 94.0744386111339
sqrt(8900.0) = 94.33981, 94.33981132056604
sqrt(8950.0) = 94.60444, 94.6044396421225
sqrt(9000.0) = 94.86833, 94.86832980505137
sqrt(9050.0) = 95.13149, 95.13148795220224
sqrt(9100.0) = 95.39392, 95.39392014169457
sqrt(9150.0) = 95.65563, 95.65563234854496
sqrt(9200.0) = 95.91663, 95.91663046625439
sqrt(9250.0) = 96.17692, 96.17692030835673
sqrt(9300.0) = 96.43651, 96.43650760992955
sqrt(9350.0) = 96.6954, 96.69539802906858
sqrt(9400.0) = 96.9536, 96.95359714832658
sqrt(9450.0) = 97.21111, 97.21111047611791
sqrt(9500.0) = 97.46794, 97.46794344808964
sqrt(9550.0) = 97.7241, 97.72410142846032
sqrt(9600.0) = 97.97959, 97.97958971132712
sqrt(9650.0) = 98.23441, 98.2344135219425
sqrt(9700.0) = 98.48858, 98.48857801796105
sqrt(9750.0) = 98.74209, 98.74208829065749
sqrt(9800.0) = 98.99495, 98.99494936611666
sqrt(9850.0) = 99.24717, 99.24716620639605
sqrt(9900.0) = 99.49874, 99.498743710662
sqrt(9950.0) = 99.74969, 99.74968671630002
sqrt(10000.0) = 100.0, 100.0
|