Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to perform square root without using math module in Python?
In this article, we will show you how to perform square root without using a math module in Python. Below are the various methods to accomplish this task:
- Using exponential operator **
- Using Mathematical Logic
Using exponential operator **
Without using the math module, the simplest approach to find the square root of a number in Python is to use the built-in exponential operator ** (It is an exponent operator because it calculates the power of the first operand to the power of the second operand).
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task ?
Create a variable to store the input number.
Use the exponential operator (**) to get the square root of a number.
Print the square root of an input number.
Example
The following program returns the square root of the input number without using math module and using ** operator ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input number</span> inputNumber <span class="token operator">=</span> <span class="token number">25</span> <span class="token comment"># getting the square root of a number using the exponential operator**</span> squareRoot <span class="token operator">=</span> inputNumber<span class="token operator">**</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment"># printing the square root of a number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The square root of"</span><span class="token punctuation">,</span> inputNumber<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span> squareRoot<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output ?
('The square root of', 25, '=', 1)
In this method, we took a number and calculated its square root with the ** operator, passing the exponent value as (1/2) or 0.5 to it, and then printed it.
Using Mathematical Logic
This method is similar to binary search.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task ?
Create a variable to store the input number.
Initialize the minimum value as 0.
Initialize maximum value with input number.
Use the for loop, to repeat the loop 10 times.
Get the middle value of both the minimum and maximum numbers by adding min and max values and dividing by 2 (Getting the average of the minimum and maximum values).
Calculate the square of the middle value using the ** operator and store it in a variable.
Use the if conditional statement to check whether the square value of the middle value is equal to the input number and if it is true then break the code using the break statement (The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C).
Else check whether the square value of the middle value is greater than the input number and if it is true then set the maximum value as the middle value.
Else if the square value of the middle value is less than the number take the minimum value as the middle value.
Print the middle value which is the resultant square root of an input number.
Example
The following program returns the square root of the input number without using math module and using mathematical logic ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input number</span>
number <span class="token operator">=</span> <span class="token number">9</span>
<span class="token comment"># initializing minimum value as 0</span>
minimum <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment"># initializing maximum value with input number</span>
maximum <span class="token operator">=</span>number
<span class="token comment"># repeating the loop 10 times using for loop</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># getting the middle value</span>
middle <span class="token operator">=</span><span class="token punctuation">(</span>minimum<span class="token operator">+</span>maximum<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span>
<span class="token comment"># getting the square of the middle value</span>
squareValue<span class="token operator">=</span>middle<span class="token operator">**</span><span class="token number">2</span>
<span class="token comment"># checking whether the square value of the middle value is equal to the number</span>
<span class="token keyword">if</span> squareValue <span class="token operator">==</span>number<span class="token punctuation">:</span>
<span class="token comment"># break the code if the condition is true</span>
<span class="token keyword">break</span>
<span class="token comment"># checking whether the square value of the middle value is more than the number</span>
<span class="token comment"># taking max value as the middle value if the condition is true</span>
maximum<span class="token operator">=</span>middle
<span class="token comment"># else if the square value of the middle value is less than the number</span>
<span class="token comment"># taking the min value as a middle</span>
minimum<span class="token operator">=</span>middle
<span class="token comment"># printing the middle value which is the resultant square root</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The resultant square root of"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">,</span> middle<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output ?
('The resultant square root of', 9, '=', 4)
Conclusion
In this article, we learned two different methods for calculating the square root of a given number without using the math module. We also learned how to calculate the square or square root using the exponential operator (**). We also computed the square using a mathematical logic method similar to the binary search.
