Python Forum
Finding square roots using long division.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding square roots using long division.
#1
Below is code for long division based finding square root.
But, it fails for decimals, as well as non-square integers.
I mean it gives the same result for 1225, and 1235, i.e. 35.
INFINITY_ = 9999999
 
def sqrtByLongDivision(n,l): 
    i = 0
    udigit, j = 0, 0 # Loop counters 
    cur_divisor = 0
    quotient_units_digit = 0
    cur_quotient = 0
    cur_dividend = 0
    cur_remainder = INFINITY_
    #print 'length of input:', len(n)
    a = [0]*l
 
    # Dividing the number into segments 
    while (n > 0): 
        a[i] = int( n % 100)
        n = int(n // 100)
        i += 1
 
    # Last index of the array of segments 
    i -= 1

    for j in range(i, -1, -1): 
        cur_remainder = INFINITY_
        cur_dividend = cur_dividend * 100 + a[j] 
 
        for udigit in range(10): 
            if (cur_dividend  - (cur_divisor * 10 + udigit) * udigit >= 0):
            #if ( (cur_remainder >(cur_dividend - ((cur_divisor * 10 + udigit) * udigit))) and (cur_dividend - ((cur_divisor * 10 + udigit) * udigit) >= 0)):
                # Calculating the remainder 
                cur_remainder = cur_dividend - ((cur_divisor * 10 + udigit)* udigit) 
                # Updating the units digit of the quotient 
                quotient_units_digit = udigit 
                if cur_remainder == 0:
                    print 'Exiting: cur_remainder ==0'
                    break
                print ('New rem. :', cur_remainder)
 
            else:
                print(' else break')
                break
 
        # Adding units digit to the quotient 
        cur_quotient = cur_quotient * 10 + quotient_units_digit 
 
        # New divisor is two times quotient 
        cur_divisor = cur_quotient * 2
 
        # Including the remainder in new dividend 
        cur_dividend = cur_remainder 
 
    return cur_quotient 
 
# Driver code 
x = 1235.789
# Find length of integer
y=x
length =0
while (y > 0): 
    y //=100
    length+= 1
print 'length :', length
 
print(sqrtByLongDivision(x, length))
Also, hope once can find the square root for non-square integers, can also find for decimals.
Reply


Messages In This Thread
Finding square roots using long division. - by jahuja73 - Feb-22-2021, 05:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Zero Division Error Leo 2 1,261 Mar-25-2022, 05:56 AM
Last Post: Leo
  Division problem Eric7Giants 1 1,704 Nov-16-2019, 05:50 AM
Last Post: ndc85430
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 2,330 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  Zero Division Error moga2003 4 3,095 Mar-07-2019, 02:15 AM
Last Post: moga2003
  Magic square! frequency 1 2,563 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Square reverse sum(overloaded) shihomiyano 6 4,115 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Perfect Square program forumer444 4 8,958 Sep-01-2017, 09:32 PM
Last Post: forumer444
  Magic Square Puzzle Harnick 1 4,889 Aug-09-2017, 04:51 PM
Last Post: nilamo
  Square Root on calculator MP1234593 1 7,970 Jun-06-2017, 06:58 PM
Last Post: nilamo
  List of square roots python py7 6 6,421 Apr-08-2017, 11:26 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020