I am very new to this and I am trying to write a program that determines whether an integer has an integer root (or not). I have combined many different ways to narrow down the search range to find the integer root including checking the ending and binary searches. Here's the code:
def endingTest (num):
print ("Now checking the input for ending that won't have integer roots")
print ()
endCheck = str(num)
lastDigit = endCheck[-1]
noIntegerSquareEnding = ["2", "3", "7", "8"]
for digit in noIntegerSquareEnding:
if lastDigit in noIntegerSquareEnding:
print ("The input ends in",lastDigit,"which can't have any integer roots")
print ("Number ending check is done \n")
print ("No integer root found.")
return False
else:
print ("Input does not end in 2, 3, 7, 8")
print ("Input ending check is done")
print ()
return True
def binarySearch (num):
print ("Now checking for the range where integer root could be found")
digits = str(num)
count = 0
for digit in digits:
count += 1
ceiling = 10**((count/2))
floor = 10**((count/2)-1)
high = int(ceiling)
low = int(floor)
print ("starting search range is between", low,"and",high)
while high-low>10:
mid = (low+high)//2
if mid**2 == num:
print ("Integer root found: ", mid, "\n")
print (steps)
return [False, None, None]
elif mid**2 > num:
high = mid
elif mid**2 < num:
low = mid
print ()
print ("Binary search is done. Search range is narrowed down to between", low, "and", high)
return [True, low, high]
def squareTest (num, low, high):
print ("Begin squaring every number from", low, "to", high)
print ()
for i in range (low, high+1):
if i**2 == num:
print ("Integer root found: ", i)
return
elif i**2 > num and i < high:
print ("The root",i,"already produces a square of",i**2, "which is higher than", num)
print ("All values from", i+1, "to", high,"are definitely out of range and therefore skipped")
print ()
print ("No integer root found")
return
elif i**2 < num and i < high:
pass
else:
print ("No integer root found")
return
def userInput ():
try:
userNumber = int(input("Enter an integer to find its integer root: "))
except:
print("That is an invalid entry. Try again.")
return None, True
else:
print ("-----------------------------")
return userNumber, False
def findIntegerRoot(testVal):
print ("Searching for integer root of", testVal)
print ("-----------------------------")
if testVal < 100:
print ("Because the number is really small, we will try squaring numbers to find root.\n")
print ("If", testVal, "has an integer square root, it would fall between 0 and half of", testVal, "plus 1\n")
squareTest (testVal, 1, (testVal//2)+1)
return
if endingTest(testVal) == False:
return
if testVal <= 2500:
print ("Because the number is relatively small, we believe it's more efficient to skip binary search step.\n")
print ("If", testVal, "has an integer square root, it would fall between 0 and half of", testVal, "plus 1\n")
squareTest (testVal, 1, (testVal//2)+1)
return
proceed = binarySearch(testVal)
if proceed[0] == False:
return
high = proceed[2]
low = proceed[1]
squareTest (testVal, low, high)
def main():
repeat = True
while repeat == True:
testVal, repeat = userInput()
findIntegerRoot(testVal)
print ("----------------------------")
print ("End of integer root search. \n")
main()
Since I am very new to programming and I'm just trying this out as a hobby, I am not sure if this program is actually efficient at finding integer root. I would imagine "efficient" means it takes the least number of steps and least amount of resources to determine the outcome. Can I please ask for some feedback?
The great thing about Python is that it offers so many built-in functions in a very simple way:
Choosing coding as a hobby was a great decision, you can find out more here.