Skip to content

Instantly share code, notes, and snippets.

@quocble
Created April 27, 2014 23:45
Show Gist options
  • Save quocble/11358247 to your computer and use it in GitHub Desktop.
Save quocble/11358247 to your computer and use it in GitHub Desktop.
Comparing square root methods - Brute force vs Babylonian method. An understanding of mathematical convergence, and time complexity
/*
The MIT License (MIT)
Copyright (c) 2014 Quoc Le <quocble@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h> /* atof */
/* sqrt(n) = x * x
We can brute force calculate the square root by picking a number x as N, and decrement
until the accuracy converges to what we what. However, the time complexity is
directly proportional to the acccuracy.
O(complexity) = n - actual_result / accuracy
So time increases as accuracy gets better and/or number gets larger. What if there is a method
to quickly converge where we can achieve both lower time & higher accuracy & support for large numbers.
*/
float sqrt_brute_force(float n) {
float result;
int count = 0;
if (n > 1) {
result = n;
while(true) {
float predicted = result * result;
float diff = n - predicted;
printf("value = %f predicted = %f diff = %f\n ", result, predicted, diff );
if (diff < 0 ) {
result = result - 0.00001;
} else {
break;
}
count++;
}
printf("count=%d\n", count);
}
else {
}
return result;
}
/*
* Babylonian Method, found in 60 AD.
* very quick convergence, and works when S < 1.0
*
* 1. Begin with an arbitrary positive starting value x0 (the closer to the actual square root of S, the better).
* 2. Let xn+1 be the average of xn and S / xn (using the arithmetic mean to approximate the geometric mean).
* 3. Repeat step 2 until the desired accuracy is achieved.
*
* http://en.wikipedia.org/wiki/Methods_of_computing_square_roots
*/
double sqrt(float S) {
printf("***");
double x0 = S / 2.0f;
double x1;
double accuracy;
do {
x1 = 0.5 * (x0 + (S/x0));
accuracy = fabs((x1* x1) - S);
printf("X1 = %f Accuracy %f\n", x1, accuracy);
x0 = x1;
}
while(accuracy > 0.000001);
return x1;
}
int main(int argc, char* argv[]) {
float x = atof(argv[1]);
printf("**** input = %f result = %f \n", x, sqrt(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment