Advertisement
impressive_i

Root n-degree

Aug 31st, 2019
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement