Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Computing the Square Root of x#

Sumit Rawal answered on May 23, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • square root copy and paste
  • javascript square root
  • su into root
  • c# square root function
  • sqrt(-4)
  • cmath sqrt
  • square root swift
  • c# Math.Sqrt
  • Reverse Square Root
  • square root of a number in java without sqrt
  • Finding square root without using sqrt function?
  • square root
  • sqrt(i)
  • square root
  • square root sorting
  • square root sorting
  • square root sorting
  • What is SQS?#
  • Finding square root without using sqrt function?
  • c# math square root

  • Computing the Square Root of x#

    0

    To compute the square root of a number, x, we first need to make an initial estimate, y, let’s say 1. We improve the estimate by taking the average of y and x/y.

    Let’s look at an example. The table below shows how Newtons method would compute the square root of 4 by approximation.

    svg viewer

    If we continue the above calculations, the average would eventually converge to 2.

    Brace yourself because below, you’ll be introduced to your first extensive Scala program!

    Implementation in Scala

    To implement the above method in Scala, we need to define five functions interdependent with each other.

    abs(): will return the absolute value of a given number

    isGoodEnough(): will let us know if the average is close enough to the actual value

    improve(): will return the average of y and x/y

    sqrtIter(): a recursive function which will compute each iteration of Newton’s method

    sqrt(): Will use the sqrtIter() function and return the square root of the given number

    When defining a function dependent on other functions, make sure to define them above the dependent function. 

    Popularity 1/10 Helpfulness 1/10 Language scala
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 23 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.