Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************
- * by impressive 31.08.19 *
- * algorithm for calculating the root of the n-th *
- * degree from an arbitrary positive number *
- **************************************************/
- #include <stdio.h>
- double mabs(double x){ return (x < 0)? -x : x; }
- int main(void) {
- double num = 8;
- int rootDegree = 3;
- printf("Число, корень которого считаем а = %f\n", num);
- printf("Корень степени n = %d\n", rootDegree);
- double eps = 0.00001; //допустимая погрешность
- double root = num / rootDegree; //начальное приближение корня
- double rn = num; //значение корня последовательным делением
- int countiter = 0; //число итераций
- while(mabs(root - rn) >= eps){
- rn = num;
- for(int i = 1; i < rootDegree; i++){
- rn = rn / root;
- }
- root = 0.5 * ( rn + root);
- countiter++;
- }
- printf("root = %f\n", root);
- printf("Число итераций = %i\n", countiter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement