How to find the square root of a number without using library function in ALGOL?

Explore methods to calculate square roots in ALGOL without using library functions. Master the techniques with step-by-step guidance.
Overview

This question delves into the mathematical computation concept of finding the square root of a number using ALGOL, an important high-level programming language. Instead of relying on built-in library functions, the question emphasizes on developing our own functional code in ALGOL. This involves understanding the numerical methods often used for such operations, such as Newton’s method or the Babylonian method, and implementing these successfully into ALGOL syntax. The discussion will further explore how to deal with the problem of precision and the complexity of the square root operation.

How to find the square root of a number without using library function in ALGOL?
Let's connect!
Meet with Alex, CEO at Anycode to get an exclusive access to anycode.ai Beta
Book a Call
Alex Hudym
CEO at Anycode

How to find the square root of a number without using library function in ALGOL?

To conduct square root calculations without using the library function in ALGOL, an iteration algorithm, such as the Babylonian method (also known as Heron's method), can be applied. This algorithm is one of the simplest and most efficient numerical techniques to produce square root approximations.

This is how we do it:

Step 1: Set an initial guess value.
Begin by determining an initial estimate of the square root. This value can be any non-negative number. A common starting guess is half the given number for which we are trying to find the square root.

  REAL x, guess;
  guess := x / 2;

Step 2: Improve the guess.
Subsequently, use this guess to perform the primary computation. The next guess is calculated as the arithmetic mean of the current guess and the quotient of the number (for which we are finding the square root) and the current guess.

  guess := (guess + (x / guess)) / 2;

Step 3: Check for convergence.
Next, the algorithm needs to check if the guess has converged, meaning that the guess is close enough to the actual square root value. We do this by computing the square of the guess and comparing it with the original number. If the absolute difference is less than a set threshold (this can be a very small number like 0.00001), then the guess has converged.

  if ABS(guess * guess - x) < 0.00001 then goto step 5;

Step 4: If the guess has not yet converged, return to Step 2.
The pseudocode looks like this:

  goto step 2;

Step 5: Once convergence has been reached, the algorithm stops, and the final guess is the square root of the number.

  PRINT('The square root of ', x, ' is approximately ', guess);

This is the entire process to find the square root of a number without using the library function in ALGOL. Despite the absence of a pre-defined function, the above steps and methods are useful in enabling calculations to be performed directly. Note that the precision of the result is influenced by both the initial guess and the threshold of convergence.

Explore other Legacy Code Languages tutorials

SNOBOL

Dive into our expansive directory of SNOBOL programming tutorials, perfect for developers intrigued by this unique string processing language. Whether you're just starting out or looking to deepen your understanding, find step-by-step guides, detailed explanations, and practical tips to master SNOBOL and enhance your programming capabilities.

Explore

Modula-2

Explore our thorough directory of Modula-2 programming tutorials, ideal for developers seeking to master this structured and robust language. Discover comprehensive guides, expert tips, and actionable advice that will elevate your Modula-2 skills, whether you're a beginner or an advanced programmer.

Explore

FORTH

Delve into our extensive directory of FORTH programming tutorials designed for developers eager to explore this unique stack-based language. From foundational concepts to advanced programming techniques, find detailed guides, practical examples, and expert insights to master FORTH and enhance your problem-solving skills in real-time programming scenarios.

Explore

CORAL 66

Discover our extensive directory of CORAL 66 programming tutorials, tailored for developers seeking to master this military and industrial-focused language. Whether you're a beginner or an experienced programmer, find step-by-step guides, detailed explanations, and practical advice to enhance your CORAL 66 programming skills and effectively tackle complex projects.

Explore

Smalltalk

Browse our extensive directory of Smalltalk tutorials designed for developers of all expertise levels. Whether you're just starting out or looking to deepen your knowledge, find step-by-step guides and practical examples to advance your skills in Smalltalk programming efficiently.

Explore

Simula

Explore our comprehensive directory of Simula programming tutorials, perfect for developers at any skill level. Discover detailed guides, expert tips, and practical solutions to enhance your Simula development skills and successfully tackle any project challenge.

Explore

RPG

Access our thorough directory of RPG programming tutorials tailored for developers of all levels. From foundational lessons to advanced techniques, explore actionable guides and expert insights that will empower your RPG development projects and elevate your coding proficiency.

Explore

Prolog

Navigate our comprehensive directory of Prolog programming tutorials designed for developers seeking to master logic-based programming. From beginner basics to advanced problem-solving techniques, find everything you need to excel in Prolog development and enhance your computational logic skills.

Explore

PL/I

Dive into our extensive directory of PL/I programming tutorials, perfect for developers at any level of experience. Uncover detailed guides, expert tips, and comprehensive resources designed to boost your PL/I development skills and help you navigate the complexities of this versatile programming language.

Explore

Pascal

Explore our detailed directory of Pascal programming tutorials, tailored for learners at all skill levels. Discover a wealth of resources, from introductory guides to advanced techniques, designed to enhance your Pascal development skills and support your programming projects effectively.

Explore

Lisp

Delve into our comprehensive directory of Lisp programming tutorials, crafted for developers from beginners to experts. Uncover the power of Lisp with in-depth guides, practical examples, and expert insights that will elevate your programming skills and help you excel in solving complex computational problems.

Explore

Fortran

Explore our extensive directory of Fortran programming tutorials, designed for engineers and scientists at every level of expertise. Learn Fortran through step-by-step guides, advanced computational techniques, and practical tips that will enhance your skills in numerical and scientific computing.

Explore

BASIC

Dive into our comprehensive directory of BASIC programming tutorials, designed for both new programmers and experienced developers. From introductory concepts to advanced programming techniques, discover the resources you need to master BASIC and create powerful, efficient applications.

Explore

ALGOL

Unlock the potential of ALGOL programming with our detailed directory of tutorials, perfect for developers seeking to explore this foundational language. Gain insights into ALGOL's structured syntax and powerful capabilities through comprehensive guides, expert advice, and practical examples tailored to enhance your coding skills.

Explore

Assembly Language

Navigate our comprehensive directory of Assembly Language programming tutorials designed for developers looking to harness the power of low-level coding. From beginner guides to advanced techniques, find detailed resources and expert tips to master Assembly Language, optimize performance, and develop efficient, hardware-level applications.

Explore

Ada

Discover our extensive directory of Ada tutorials tailored for developers of all skill levels. Navigate through beginner guides, advanced programming techniques, and practical examples to effectively enhance your Ada development skills and tackle complex projects with confidence.

Explore