Hướng dẫn newtons method square root python - phương pháp newton căn bậc hai python

Cải thiện bài viết

Show

    Lưu bài viết

    Với số nguyên n và mức dung sai L, nhiệm vụ là tìm căn bậc hai của số đó bằng phương thức Newton.N and a tolerance level L, the task is to find the square root of that number using Newton’s Method.
    Examples: 
     

    Đầu vào: n = 16, l = 0,0001 & nbsp; đầu ra: 4 & nbsp; 42 = 16Input: n = 327, l = 0,00001 & nbsp; đầu ra: 18.0831 & nbsp; & nbsp; N = 16, L = 0.0001 
    Output:
    42 = 16
    Input: N = 327, L = 0.00001 
    Output: 18.0831 
     

    Phương thức Newton Newton: & nbsp; Đặt n là bất kỳ số nào sau đó là căn bậc hai của n có thể được đưa ra bởi công thức: & nbsp; & nbsp; 
    Let N be any number then the square root of N can be given by the formula: 
     

    root = 0,5 * (x + (n / x)) trong đó x là bất kỳ dự đoán nào có thể được coi là n hoặc 1. & nbsp; & nbsp;
     

    • Trong công thức trên, x là bất kỳ căn bậc hai giả định của N và gốc là căn bậc hai chính xác của N.X is any assumed square root of N and root is the correct square root of N.
    • Giới hạn dung sai là sự khác biệt tối đa giữa X và gốc cho phép.X and root allowed.

    Cách tiếp cận: Các bước sau đây có thể được tuân thủ để tính toán câu trả lời: & nbsp; & nbsp; The following steps can be followed to compute the answer: 
     

    1. Gán x cho chính n.X to the N itself.
    2. Bây giờ, hãy bắt đầu một vòng lặp và tiếp tục tính toán gốc chắc chắn sẽ di chuyển về phía căn bậc hai của N.root which will surely move towards the correct square root of N.
    3. Kiểm tra sự khác biệt giữa X được giả định và gốc được tính toán, nếu chưa có dung sai bên trong sau đó cập nhật gốc và tiếp tục.X and calculated root, if not yet inside tolerance then update root and continue.
    4. Nếu gốc được tính toán bên trong dung sai cho phép thì hãy thoát ra khỏi vòng lặp.root comes inside the tolerance allowed then break out of the loop.
    5. In gốc.root.

    Dưới đây là việc thực hiện phương pháp trên: & nbsp; & nbsp;
     

    C++

    #include <bits/stdc++.h>

    using namespace std;

    double squareRoot(double n, float l)

    using0

    using1double using3

    using1double using6

    using1using8 using9

    using1namespace1 namespace2

    namespace3namespace4

    namespace3namespace6

    namespace3namespace8 namespace9std;0std;1

    std;2std;3std;4

    namespace3std;6

    using1std;8

    using1double0 using6

    std;8

    using8 double4

    using0

    using1double double8

    using1float squareRoot(1

    using1squareRoot(3

    using1double0 squareRoot(6

    std;8

    Java

    squareRoot(8 squareRoot(9

    using0

    using1double2 double squareRoot(double n, double l)

    using1using0

    namespace3double using3

    namespace3double using6

    namespace3using8 n, 9float0std;4

    namespace3namespace1 namespace9float55____86

    namespace3using0

    std;2namespace4

    std;2l)2l)3 l)4

    std;2namespace8 l)7

    l)8std;3std;4

    std;2std;6

    namespace3std;8

    namespace3double0 using6

    using1std;8

    using1using11 double2 using13 using14

    using1using0

    namespace3double using19using20std;4

    namespace3double using24using25std;4

    namespace3using28

    using1std;8

    std;8

    Python3

    using32 using33

    using1using35using36 using37

    using1using39using36 float0

    using1namespace1 namespace9using45using46

    namespace3using39using49using36 using45

    namespace3using53using36 l)3 using56 using57__

    namespace3namespace8 namespace9std;0using66using67 using68

    std;2std;3

    namespace3using35using36 using74

    using1double0 using74

    namespace8 using79using36using36 using82 using83

    using1using85using36 using20

    using1using89using36 using25

    using1using93using94

    C#

    using using96

    squareRoot(8 squareRoot(9

    using0

    using1double2 double squareRoot(double n, double l)

    using1using0

    namespace3double using3

    namespace3double using6

    namespace3using8 n, 9float0std;4

    namespace3namespace1 namespace9float55____86

    namespace3using0

    std;2namespace4

    std;2namespace6

    std;2l)2l)3 l)4

    l)8std;3std;4

    std;2std;6

    namespace3std;8

    namespace3double0 using6

    using1std;8

    using1using11 double2 using13 using14

    using1using0

    namespace3double using19using20std;4

    namespace3double using24using25std;4

    namespace3namespace59

    using1std;8

    std;8

    std;8

    namespace63

    using32 using33

    using1using0

    namespace3namespace70

    namespace3namespace72

    namespace3namespace74

    namespace3namespace1 namespace9float55____86

    namespace3using0

    std;2namespace4

    std;2namespace6

    std;2namespace8 l)7

    l)8std;3std;4

    std;2std;6

    namespace3std;8

    namespace3double0 using6

    using1std;8

    using1std;02

    using1std;04

    using1using11 double2 using13 using14

    std;07

    namespace3double using19using20std;4

    namespace3double using24using25std;4