Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 210717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:01:50+00:00 2026-05-11T18:01:50+00:00

I know this sounds like a homework assignment, but it isn’t. Lately I’ve been

  • 0

I know this sounds like a homework assignment, but it isn’t. Lately I’ve been interested in algorithms used to perform certain mathematical operations, such as sine, square root, etc. At the moment, I’m trying to write the Babylonian method of computing square roots in C#.

So far, I have this:

public static double SquareRoot(double x) {
    if (x == 0) return 0;

    double r = x / 2; // this is inefficient, but I can't find a better way
                      // to get a close estimate for the starting value of r
    double last = 0;
    int maxIters = 100;

    for (int i = 0; i < maxIters; i++) {
        r = (r + x / r) / 2;
        if (r == last)
            break;
        last = r;
    }

    return r;
}

It works just fine and produces the exact same answer as the .NET Framework’s Math.Sqrt() method every time. As you can probably guess, though, it’s slower than the native method (by around 800 ticks). I know this particular method will never be faster than the native method, but I’m just wondering if there are any optimizations I can make.

The only optimization I saw immediately was the fact that the calculation would run 100 times, even after the answer had already been determined (at which point, r would always be the same value). So, I added a quick check to see if the newly calculated value is the same as the previously calculated value and break out of the loop. Unfortunately, it didn’t make much of a difference in speed, but just seemed like the right thing to do.

And before you say “Why not just use Math.Sqrt() instead?”… I’m doing this as a learning exercise and do not intend to actually use this method in any production code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:01:50+00:00Added an answer on May 11, 2026 at 6:01 pm

    First, instead of checking for equality (r == last), you should be checking for convergence, wherein r is close to last, where close is defined by an arbitrary epsilon:

    eps = 1e-10  // pick any small number
    if (Math.Abs(r-last) < eps) break;
    

    As the wikipedia article you linked to mentions – you don’t efficiently calculate square roots with Newton’s method – instead, you use logarithms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this sounds like a really obvious question, but it's proving harder to
I know this sounds like a broad question but I can narrow it down
This sounds like a simple question, but I don't know how to search for
I know this isn't strictly a programming question but y'all must have experienced this.
I know this sounds like a really, really simple use case and I'm hoping
I know this is subjective, but I'd like to know peoples opinions and hopefully
I know this sounds rediculously easy but there is simply no documentation about this
(Sorry if this sounds like a rant, but it's a real question and I'd
This sounds like a weird title and probably not stated too well. But here's
Does anyone know how to change the brush for a menu's background? This sounds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.