Want more features on Pastebin? Sign Up, it's FREE!
Guest

Integral Nth root Newton Raphson

By: a guest on Jan 11th, 2012  |  syntax: C  |  size: 0.44 KB  |  views: 254  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int root( int A, int n )
  5. {
  6.         int ret, s;
  7.  
  8.         ret = 1; /* Initial guess */
  9.         s = 1;
  10.  
  11.         for ( ; ; )
  12.         {
  13.                 ret = ( ( n - 1 ) * ret + ( A / ( int )pow( ret, n - 1 ) ) ) / n;
  14.  
  15.                 if ( s == ret )
  16.                 {
  17.                         break;
  18.                 }
  19.                 else if ( s != ret - 1 )
  20.                 {
  21.                         s = ret;
  22.                 }
  23.         }
  24.  
  25.         return ret;
  26. }
  27.  
  28. int main( int argc, char* argv[] )
  29. {
  30.         printf( "%ith root of %i = %i\n", 4, 103, root( 103, 4 ) );
  31.  
  32.         return 0;
  33. }
clone this paste RAW Paste Data