Skip to content

Instantly share code, notes, and snippets.

@aaani
Last active December 19, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaani/5989373 to your computer and use it in GitHub Desktop.
Save aaani/5989373 to your computer and use it in GitHub Desktop.
Here is an implementation of the Newton-Raphson method for finding square root. Time complexity analysis: http://en.citizendium.org/wiki/Newton%27s_method#Computational_complexity
// Newton-Raphson
//
// Created by Anirvana Mishra on 6/29/13.
// Copyright (c) 2013 Anirvana Mishra. All rights reserved.
//
long double sq_root(double x)
{
long double rt = 1, ort = 0;
while(ort!=rt)
{
ort = rt;
rt = ((x/rt) + rt) / 2;
}
return rt;
}
int main(int argc, const char * argv[]) //Driver
{
cout<<sq_root(224654);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment