The Wayback Machine - https://web.archive.org/web/20240605070559/https://www.geeksforgeeks.org/square-root-of-a-number-by-repeated-subtraction-method/
Open In App

Square root of a number by Repeated Subtraction method

Last Updated : 26 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Repeated Subtraction method for finding the square root of a number is an interesting approach. It involves repeatedly subtracting consecutive odd numbers from the number until you reach zero or a negative number. The number of times you can subtract is the approximate square root of the original number.

Given an integer N, the task is to find its perfect square root by repeated subtraction only.
Examples:

Input: N = 25 
Output: 5
Input: N = 841 
Output: 29 
 
 


Babylonian Method and Binary Search Approach: Refer to Square root of an integer for the approaches based on Babylonian Method and Binary Search.
Repeated Subtraction Approach: 
Follow the steps below to solve the problem: 
 

  • Sum of the first N odd natural numbers is equal to N2. 
     
  • Based on the fact mentioned above, repetitive subtraction of odd numbers starting from 1, until N becomes 0 needs to be performed. 
     
  • The count of odd numbers, used in this process, will give the square root of the number N. 

Illustration: 
N = 81
Step 1: 81-1=80 
Step 2: 80-3=77 
Step 3: 77-5=72 
Step 4: 72-7=65 
Step 5: 65-9=56 
Step 6: 56-11=45 
Step 7: 45-13=32 
Step 8: 32-15=17 
Step 9: 17-17=0
Since, 9 odd numbers were used, hence the square root of 81 is 9. 
 


Below is the implementation of the above approach.
 

C++
// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to return the square
// root of the given number
int SquareRoot(int num)
{
    int count = 0;

    for (int n = 1; n <= num; n += 2) {

        // Subtract n-th odd number
        num = num - n;
        count += 1;
        if (num == 0)
            break;
    }

    // Return the result
    return count;
}

// Driver Code
int main()
{
    int N = 81;
    cout << SquareRoot(N);
}
Java
// Java implementation of 
// the above approach 
class GFG{
    
// Function to return the square 
// root of the given number 
public static int SquareRoot(int num) 
{ 
    int count = 0; 
    
    for(int n = 1; n <= num; n += 2) 
    { 

       // Subtract n-th odd number 
       num = num - n; 
       count += 1; 
       if (num == 0) 
           break; 
    } 
    
    // Return the result 
    return count; 
} 

// Driver code    
public static void main(String[] args)
{
    int N = 81; 
    System.out.println(SquareRoot(N));
}
}

// This code is contributed by divyeshrabadiya07
Python
# Python3 implementation of the
# above approach

# Function to return the square
# root of the given number
def SquareRoot(num):
    
    count = 0
    for n in range(1, num + 1, 2):
        
        # Subtract n-th odd number
        num = num - n
        count = count + 1
        if (num == 0):
            break

    # Return the result
    return count

# Driver Code
N = 81
print(SquareRoot(N))

# This code is contributed by Sanjit_Prasad
C#
// C# implementation of 
// the above approach 
using System;

class GFG{
    
// Function to return the square 
// root of the given number 
public static int SquareRoot(int num) 
{ 
    int count = 0; 
    
    for(int n = 1; n <= num; n += 2) 
    { 
        
        // Subtract n-th odd number 
        num = num - n; 
        count += 1; 
        if (num == 0) 
            break; 
    } 
    
    // Return the result 
    return count; 
} 

// Driver code 
public static void Main()
{
    int N = 81; 
    
    Console.Write(SquareRoot(N));
}
}

// This code is contributed by chitranayal
Javascript
<script>

// Javascript implementation of 
// the above approach 

// Function to return the square 
// root of the given number 
function SquareRoot(num) 
{ 
    let count = 0; 

    for (let n = 1; n <= num; n += 2) { 

        // Subtract n-th odd number 
        num = num - n; 
        count += 1; 
        if (num == 0) 
            break; 
    } 

    // Return the result 
    return count; 
} 

// Driver Code 

    let N = 81; 
    document.write(SquareRoot(N)); 

// This code is contributed by Mayank Tyagi

</script>

Output
9

Time Complexity: O(N) 
Auxiliary Space: O(1)
 



Similar Reads

Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
Given an integer N, the task is to find the digital root N2 using the digital root of N. Digital Root of a positive integer is calculated by adding the digits of the integer. If the resultant value is a single digit, then that digit is the digital root. If the resultant value contains two or more digits, those digits are summed and the process is r
6 min read
Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element
Given an array arr[] consisting of N integers, the task is to find the remaining array element after subtracting each element from its next adjacent element and removing the last array element repeatedly. Examples: Input: arr[] = {3, 4, 2, 1}Output: 4Explanation:Operation 1: The array arr[] modifies to {4 - 3, 2 - 4, 1 - 2} = {1, -2, -1}.Operation
8 min read
Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
Given an array arr[] consisting of N integers, the task is to make all array elements equal by selecting any pair of integers from the array and replacing the larger integer from the pair with their absolute difference any number of times. Print the final value of all array elements. Examples: Input: arr[] ={2, 3, 4}Output: 1Explanation: Step 1: Pe
5 min read
Find K such that repeated subtraction of K from Array elements make the Array equal
Given an array arr[] of size N, the task is to find the value of an integer K such that its repeated subtraction from array elements will make all array elements in minimum operations. Example: Input: arr[] = {5, 3, 3, 7}Output: 2Explanation: Minimum 2 operations must be performed:1st operation: subtract 2 from elements at indices {0, 3}, arr[] = {
5 min read
Repeated subtraction among two numbers
Given a pair of positive numbers x and y. We repeatedly subtract the smaller of the two integers from greater one until one of the integers becomes 0. The task is to count number of steps to before we stop (one of the numbers become 0). Examples : Input : x = 5, y = 13 Output : 6 Explanation : There are total 6 steps before we reach 0: (5,13) --
6 min read
Find maximum array sum after making all elements same with repeated subtraction
Given an array of n elements, find out the maximum possible sum of all elements such that all the elements are equal. Only operation allowed is choosing any two elements and replacing the larger of them by the absolute difference of the two. Examples: Input : 9 12 3 6 Output : 12 Explanation : 9 12 3 6 replace a2 = 12 with a2-a4 = 12 - 6 =&gt; 6 i.
6 min read
Check if a number is perfect square without finding square root
Check whether a number is a perfect square or not without finding its square root. Examples: Input: n = 36 Output: Yes Input: n = 12 Output: No Recommended PracticeCheck perfect squareTry It! Method 1:The idea is to run a loop from i = 1 to floor(sqrt(n)) and then check if squaring it makes n. Below is the implementation: C/C++ Code // C++ program
9 min read
Check perfect square using addition/subtraction
Given a positive integer n, check if it is perfect square or not using only addition/subtraction operations and in minimum time complexity.We strongly recommend you to minimize your browser and try this yourself first.We can use the property of odd number for this purpose: Addition of first n odd numbers is always perfect square 1 + 3 = 4, 1 + 3 +
4 min read
Calculate sum of the main diagonal and the number of rows and columns containing repeated values in a square matrix
Given a matrix M[][] of dimensions N * N, consisting only of integers from the range 1 to N, the task is to compute the sum of the matrix elements present in the main diagonal, the number of rows and columns containing repeated values. Examples: Input: N = 4, M[][] = {{1, 2, 3, 4}, {2, 1, 4, 3}, {3, 4, 1, 2}, {4, 3, 2, 1}}Output: 4 0 0Explanation:S
14 min read
Fast method to calculate inverse square root of a floating point number in IEEE 754 format
Given a 32-bit floating point number x stored in IEEE 754 floating point format, find the inverse square root of x, i.e., x-1/2.A simple solution is to do floating point arithmetic. Following is an example function. C/C++ Code #include &lt;bits/stdc++.h&gt; using namespace std; float InverseSquareRoot(float x) { return 1/sqrt(x); } int main() { cou
4 min read