impressive_i

Root n-degree

Aug 31st, 2019
200
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**************************************************
  2.  * by impressive 31.08.19                         *
  3.  * algorithm for calculating the root of the n-th *
  4.  * degree from an arbitrary positive number       *
  5.  **************************************************/
  6.  
  7. #include <stdio.h>
  8.  
  9. double mabs(double x){ return (x < 0)? -x : x; }
  10.  
  11. int main(void) {
  12.     double num = 8;
  13.     int rootDegree = 3;
  14.  
  15.     printf("Число, корень которого считаем а = %f\n", num);
  16.     printf("Корень степени n = %d\n", rootDegree);
  17.    
  18.     double eps = 0.00001;              //допустимая погрешность
  19.     double root = num / rootDegree;    //начальное приближение корня
  20.     double rn = num;                   //значение корня последовательным делением
  21.     int countiter = 0;                 //число итераций
  22.     while(mabs(root - rn) >= eps){
  23.         rn = num;
  24.         for(int i = 1; i < rootDegree; i++){
  25.             rn = rn / root;
  26.         }
  27.         root = 0.5 * ( rn + root);
  28.         countiter++;
  29.     }
  30.    
  31.     printf("root = %f\n", root);
  32.     printf("Число итераций = %i\n", countiter);
  33.    
  34.     return 0;
  35. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×