Given an integer N, the task is to find its perfect square root by repeated subtraction only.
Input: N = 25
Input: N = 841
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.
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.
Time Complexity: O(N)
Auxiliary Space: O(1)
- Repeated subtraction among two numbers
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Check if a number is perfect square without finding square root
- Check perfect square using addition/subtraction
- Digital Root (repeated digital sum) of the given large integer
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Square root of a number using log
- Find square root of number upto given precision using binary search
- Square root of a number without using sqrt() function
- C program to find square root of a given number
- Min operations to reduce N by multiplying by any number or taking square root
- Babylonian method for square root
- Long Division Method to find Square root with Examples
- Find smallest perfect square number A such that N + A is also a perfect square number
- Square root of an integer
- Range Minimum Query (Square Root Decomposition and Sparse Table)
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.