Root Long Division

using the long division method to find a square root

user@linux-pc:~$ python sqrt_long.py -i 12
     
The radicand is divided into chunks of two digits. 
The decimal point is wherever you place it. To simplify multiply by multiples of 100.     
     
      3 4 6 4 1
    ____________________
__ / 12
  V   9
   ----
      300
      256  = ( 3 x 20 + 4 ) x  4
   ------
       4400
       4116  = ( 34 x 20 + 6 ) x  6
     ------
        28400
        27696  = ( 346 x 20 + 4 ) x  4
       ------
          70400
          69281  = ( 3464 x 20 + 1 ) x  1
         ------
           111900

method

python script


# /bin/python 
# ---------------------------------------
# long division method for finding roots 
# ---------------------------------------

## I struggled with getting the steps as independent entities. They are all tied together so I only did five steps.

import math
import re
import os
import sys
import string
from Tkinter import *
from optparse import OptionParser

# TODO the following function is still work in progress
def chunk_radicand(x_value):
	new_x_2 = float(x_value)
	length_2 = len(str(int(new_x_2)))
	chunk_2 = {}
	rest_2 = {}
	j_2 = 0
	k_2 = int(length_2) / 2
	if (length_2 % 2 == 0):
		while (j_2 <= length_2/2):
			factor_2 = 10**((k_2-1-j_2)*2)
			chunk_2[j_2] = new_x_2 // factor_2
			rest_2[j_2] = new_x_2 % factor_2
			new_x_2 = new_x_2 - chunk_2[j_2]*factor_2
			j_2 = j_2 + 1 
	else:
		while (j_2 <= length_2 / 2):
			factor_2 = 10**((k_2-j_2)*2)
			chunk_2[j_2] = new_x_2 // factor_2
			rest_2[j_2] = new_x_2 % factor_2
			new_x_2 = new_x_2 - chunk_2[j_2]*factor_2
			j_2 = j_2 + 1 


def calc_root(x_value):
	new_x = float(x_value)
	length = len(str(int(new_x)))
	chunk = {}
	rest = {}
	p =int()
	list_1 = {}
	list_2 = {}
	e = 0
	while (e < 3):
		list_1[e] = (e+1)**2
		e = e + 1
	
	f = 0
	while (f < 7):
		list_2[f] = (f+3)**2
		f = f + 1

	
#	----------------------------------------
#	chunking into portions of 2 digits
#	----------------------------------------


	j = 0
	k = int(length) / 2
	if (length % 2 == 0):
		while (j <= length/2):
			factor = 10**((k-1-j)*2)
			chunk[j] = new_x // factor
			rest[j] = new_x % factor
			new_x = new_x - chunk[j]*factor
			j = j + 1 
	else:
		while (j <= length/2):
			factor = 10**((k-j)*2)
			chunk[j] = new_x // factor
			rest[j] = new_x % factor
			new_x = new_x - chunk[j]*factor
			j = j + 1 
			
#	------------------------------------------
#	the algorithm
#	------------------------------------------
	subtrahend = {}
	subtrahend_0 = {}
	subtrahend_1 = {}
	subtrahend_2 = {}
	subtrahend_3 = {}
	subtrahend_4 = {}
	current_root = {}
	difference = {}
	minuend = {}
	root_digit = {}
	
	if (chunk[0] < list_1[1]):
		max_square = list_1[0]
		count = 1
	else:
		if (chunk[0] < list_1[2]):
			max_square = list_1[1]
			count = 2
		else:
			if (chunk[0] == list_1[2]):
				max_square = list_1[2]
				count = 3


	if (length % 2 == 0):
		if (chunk[0] < list_2[1]):
			max_square = list_2[0]
			count = 3
		else:
			if (chunk[0] < list_2[2]):
				max_square = list_2[1]
				count = 4
			else:
				if (chunk[0] < list_2[3]):
					max_square = list_2[2]
					count = 5
				else:
					if (chunk[0] < list_2[4]):
						max_square = list_2[3]
						count = 6
					else:
						if (chunk[0] < list_2[5]):
							max_square = list_2[4]
							count = 7
						else:
							if (chunk[0] < list_2[6]):
								max_square = list_2[5]
								count = 8
							else:
								max_square = list_2[6]
								count = 9
	difference[0] = chunk[0] - max_square
	if (x_value < 10):
		minuend[0] = int(difference[0]*100)
	else:
		minuend[0] = int(difference[0]*100 + chunk[1])
	root_digit[0] = count
	current_root[0] = int(root_digit[0])
	subtrahend_0[0] = (20*current_root[0]+0) * 0
	
	u = 0
	while (subtrahend_0[u] <= minuend[0]):
		u = u + 1
		subtrahend_0[u] = (20*current_root[0]+u) * u
		#print subtrahend_0
		root_digit[1] = u-1
	#print root_digit[1]
	#print difference[0]
	
#   2nd step
	
	difference[1] = minuend[0] - subtrahend_0[root_digit[1]]
	#print difference[1]
	#if (difference[1] == 0):
	#	print ""
	#	print "The root is ", root_digit[0] , root_digit[1]
	#	print ""
	#else:
	if (x_value < 1000):
		minuend[1] = int(difference[1]*100)
	else:
		minuend[1] = int(difference[1]*100 + chunk[2])
	#print minuend[1]
	current_root[1] = int(10*root_digit[0] + root_digit[1])
	subtrahend_1[0] = (20*current_root[1]+0) * 0
	w = 0
	while (subtrahend_1[w] < minuend[1]):
		w = w + 1
		subtrahend_1[w] = (20*current_root[1]+w) * w
	#print subtrahend_1
	root_digit[2] = w-1
	
#	3rd step
#
#	TODO make the algorithm more modular. i.e. turn steps into functions. throw an error.
#
	difference[2] = minuend[1] - subtrahend_1[root_digit[2]]
	#print difference[2]
	if (x_value < 100000):
		minuend[2] = int(difference[2]*100)
	else:
		minuend[2] = int(difference[2]*100 + chunk[3])
	#print minuend[2]
	current_root[2] = int( 10*current_root[1]+root_digit[2])
	subtrahend_2[0] = (20*current_root[2]+0) * 0
	
	y = 0
	while (subtrahend_2[y] < minuend[2]):
		y = y + 1
		subtrahend_2[y] = (20*current_root[2]+y) * y
	#print subtrahend_2
	root_digit[3] = y-1
	
	
#	4th step

	difference[3] = minuend[2] - subtrahend_2[root_digit[3]]
	#print difference[3]
	if (x_value < 10*10**(2*6)):
		minuend[3] = int(difference[3]*100)
	else:
		minuend[3] = int(difference[3]*100 + chunk[4])
	#print minuend[3]
	current_root[3] = int( 10*current_root[2]+root_digit[3])
	subtrahend_3[0] = (20*current_root[3]+0) * 0
	
	p = 0
	while (subtrahend_3[p] < minuend[3]):
		p = p + 1
		subtrahend_3[p] = (20*current_root[3]+p) * p
	#print subtrahend_3
	root_digit[4] = p-1
		

#	5th step

	difference[4] = minuend[3] - subtrahend_3[root_digit[4]]
	#print difference[3]
	minuend[4] = int(difference[4]*100 )
	#print minuend[4]
	current_root[4] = int( 10*current_root[3]+root_digit[4])
	subtrahend_4[0] = (20*current_root[4]+0) * 0
	
	r = 0
	while (subtrahend_4[r] < minuend[4]):
		r = r + 1
		subtrahend_4[r] = (20*current_root[4]+r) * r
	#print subtrahend_4
	root_digit[5] = r-1
	#return root_digit[5]

#	------------------------------------------
#	The following section sorts out the output TODO
#	------------------------------------------
		
	print "     "
	print "The radicand is divided in to chunks of two digits beginning at the right."
	print "The decimal point is wherever you place it. To simply multiply by multiples of 100.     "
	print "     "
	if (length % 2  == 1):
		print "    ",'{:>2}'.format(root_digit[0]),root_digit[1],root_digit[2],root_digit[3],root_digit[4]
	else:
			print "   ",'{:>3}'.format(root_digit[0]),root_digit[1],root_digit[2],root_digit[3],root_digit[4]
	if (length % 2  == 1):
		print "    ____________________"
		print ("__ / "),	
		print '{:<2}'.format(x_value)
		print "  V  ",'{:<2}'.format(max_square)," = ",root_digit[0],"x", root_digit[0] #'{:>20} {:<20}'.format(new_a,b)
		print "   ----"
		print "    ",'{:>4}'.format(minuend[0])
		print "    ", '{:>4}'.format(subtrahend_0[u-1])," = (",root_digit[0],"x",20,"+", root_digit[1],") x ",root_digit[1]
		print "     -----"
		print "    ",'{:>6}'.format(minuend[1])
		print "    ",'{:>6}'.format(subtrahend_1[w-1])," = (",current_root[1],"x",20,"+", root_digit[2],") x ",root_digit[2]
		print "     -------"
		print "    ",'{:>8}'.format(minuend[2])
		print "    ",'{:>8}'.format(subtrahend_2[y-1])," = (",current_root[2],"x",20,"+", root_digit[3],") x ",root_digit[3]
		print "     ---------"
		print "    ",'{:>10}'.format(minuend[3])
		print "    ",'{:>10}'.format(subtrahend_3[p-1])," = (",current_root[3],"x",20,"+", root_digit[4],") x ",root_digit[4]
		print "     -----------"
		print "    ",'{:>12}'.format(minuend[4])
	else:
		print "    ____________________"
		print ("__ /"),	
		print '{:>2}'.format(x_value)
		print "  V ",'{:>2}'.format(max_square)," = ",root_digit[0],"x", root_digit[0] #'{:>20} {:<20}'.format(new_a,b) #'{:>20} {:<20}'.format(new_a,b)
		print "    ---"
		print "    ",'{:>4}'.format(minuend[0])
		print "    ", '{:>4}'.format(subtrahend_0[u-1])," = (",root_digit[0],"x",20,"+", root_digit[1],") x ",root_digit[1]
		print "    -----"
		print "    ",'{:>6}'.format(minuend[1])
		print "    ",'{:>6}'.format(subtrahend_1[w-1])," = (",current_root[1],"x",20,"+", root_digit[2],") x ",root_digit[2]
		print "    -------"
		print "    ",'{:>8}'.format(minuend[2])
		print "    ",'{:>8}'.format(subtrahend_2[y-1])," = (",current_root[2],"x",20,"+", root_digit[3],") x ",root_digit[3]
		print "    ---------"
		print "    ",'{:>10}'.format(minuend[3])
		print "    ",'{:>10}'.format(subtrahend_3[p-1])," = (",current_root[3],"x",20,"+", root_digit[4],") x ",root_digit[4]
		print "    -----------"
		print "    ",'{:>12}'.format(minuend[4])
	print "     "
	print "     "
#
#
#   main section
#
def main():
    parser = OptionParser(usage='"%prog -i root"')
    parser.add_option("-i", dest="sqroot", help="enter the root you want to calculate")
    (options, args) = parser.parse_args()
    if not options.sqroot:
        parser.error('no root given, use -i [square root]')
    if options.sqroot:
		sq_value = int(options.sqroot)
		sq_list = {1,4,9,16,25,36,49,64,81,100,121,144}
		if sq_value not in sq_list:
			calc_root(sq_value)
		else:
			print "---------------------"
			print " "
			print sq_value, "is a square number"
			print ""
			print "---------------------"


if __name__ == "__main__":
    main()
    

(c) 2025 sebastian.williams[at]sebinberlin.de - impressum und datenschutz - Powered by MathJax & XMin & HUGO & jsxgraph & mypaint