Cubic Root Solution (Newton iterative method)

Source: Internet
Author: User


Computes the cubic root of a number without using library functions

Detailed Description:

Interface description

Prototype:

public static double Getcuberoot (double input)

Input: Double to solve parameter

Return value: Double the cube root of the input parameter



Input Description:

Parameter double type to solve



Output Description:

The cube root of the input parameter is also a double type


Input Example:
216


Output Example:
6.0





importjava.util.Scanner;

Common methodspublic class Main {    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);        while(cin.hasNext()) {            double input = cin.nextDouble();            double min = 0;            double max = input;            while(max - min > 0.00001) {                double temp = (min + max) / 2;                                if(temp*temp*temp > input) {                    max = temp;                } else{                    min = temp;                }            }            min*=10;            double small = min - (int)min;            if(small >= 0.5) {                min++;            }            int n = (int)min;            min=(double)n/10;            System.out.println(min);        }    }}


Newton Iteration

  1. #include <stdio.h>

  2. //#include <stdlib.h>

  3. #define JINGDU 0.0001

  4. double Newton_diedai ( double a) {

  5. double xn,xn_1;

  6. Xn=1;

  7. xn_1 = xn-((xn*xn*xn-a)/(3*XN*XN));

  8. while (Xn-xn_1>jingdu | | xn-xn_1<-jingdu) {

  9. xn = xn_1;

  10. xn_1 = xn-((xn*xn*xn-a)/(3*XN*XN));

  11. }

  12. return xn_1;

  13. }

  14. int Main ( void ) {

  15. double A;

  16. scanf ("%lf" , &a);

  17. A = Newton_diedai (a);

  18. printf ("%.1f" , a);

  19. System ("pause" );

  20. return 0;

  21. }


This article is from "Go far." blog, declined to reprint!

Cubic Root Solution (Newton iterative method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.