We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target.
As the first step in our calculation, we take a target number and find its square root (that is, the number that when multiplied by itself equals the target number).
The journey will be very long. To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient. Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power. Instead we want to implement our own square root calculation.
Your task is to calculate the square root of a given number.
Some potential approaches:
You can check out the Wikipedia pages on integer square root and methods of computing square roots to help with choosing a method of calculation.
This problem can be solved easily using Roc's built-in Num
module, including the Num.sqrt
function.
However, we'd like you to consider the challenge of solving this exercise without using built-ins or modules.
While there is a mathematical formula that will find the square root of any number, we have gone the route of having only natural numbers (positive integers) as solutions.
Sign up to Exercism to learn and master Roc with 105 exercises, and real human mentoring, all for free.