#==================================================
# Lecture 15: Searching and Approximation Methods
#--------------------------------------------------
#
# Slide # 8:

x = int(input("Enter an integer: "))

found = False
root = 0
for guess in range(x + 1):    #<-- x + 1, so that x itself (e.g. 1) is tried
    if guess**2 == x:
        root = guess
        found = True
        # break         #<-- Optionally, we can stop the loop immediately

if found:
    print("Square root of", x, "is", root)
else:
    print(x, "is not a perfect square")

#--------------------------------------------------
#
# Slide # 10:

n = int(input("Enter an integer between 1 and 100: "))

found = False       #<-- A boolean variable to keep track of the search result
guess = 0           #<-- Our initial guess, also acting as a loop variable

while guess <= 100:
    if guess == n:
        found = True
        break
    guess += 1

if found:
    print("You guessed: ", guess)
else:
    print("I couldn't find your number between 1 and 100")

#----------------
# OR - Using for loop:

n = int(input("Enter an integer between 1 and 100: "))

found = False
for guess in range(101):
    if guess == n:
        found = True
        break

if found:
    print("You guessed: ", guess)
else:
    print("I couldn't find your number between 1 and 100")

#--------------------------------------------------
# Slide # 21:

aliTickets = 0
found = False
for aliGuess in range(18):
    for fatimaGuess in range(18):
        if aliGuess + fatimaGuess == 17 and aliGuess - fatimaGuess == 3:
            aliTickets = aliGuess
            found = True

if found:
    print(f"Ali sold {aliTickets} tickets")
else:
    print("No solution found")

#--------------------------------------------------
# Slide # 24:

for ali in range(11):
    for zain in range(11):
        for fatima in range(11):
            total = (ali + zain + fatima == 10)
            two_less = (zain == ali-2)
            twice = (fatima == 2*ali)
            if total and two_less and twice:
                print(f"Ali sold {ali} tickets")
                print(f"Zain sold {zain} tickets")
                print(f"Fatima sold {fatima} tickets")

#--------------------------------------------------

#==================================================
# Approximation Methods
#--------------------------------------------------
# Slide # 27:

x = 0
for i in range(10):
    x += 0.1
print(x == 1)
print(x, '==', 10*0.1)

#--------------------------------------------------
#
# Slide 44: (Slow Sqrt, Sequential Search)

n = int(input("Enter an integer: "))

found = False
increment = 0.0000001
epsilon = 0.000001
guess = 0
tries = 0
while guess**2 < n:
    if (guess**2 >= n-epsilon) and (guess**2 <= n+epsilon):
        found = True
        break
    guess += increment
    tries += 1
    
if found:
    print(f"Square root of {n} is {guess}, in {tries} tries")
else:
    print(f"Couldn't find the square root of {n}")

#--------------------------------------------------
# Slide 54: (Fast Sqrt, Binary Search)

n = 9999999999 # int(input())

low = 0             #<-- one below the smallest possible number
high = 10000000001  #<-- one above the largest possible number
guess = (low + high)//2
found = False
tries = 0
while high - low > 1:   #<-- stop when no whole number is left between them
    if guess == n:
        found = True
        break
    elif guess < n:
        low = guess
    else:			# guess > n
        high = guess
    guess = (low + high)//2
    tries += 1
    
if found:
    print(f"Found: {guess} in {tries} tries")
else:
    print(f"Not Found")

#--------------------------------------------------
# Slide 25:

n = int(input("Enter an integer: "))

epsilon = 0.00000000000001
low = 0
high = n
guess = (low + high)/2
tries = 0
while high-low > epsilon:
    if guess**2 < n:
        low = guess
    else:
        high = guess
    guess = (low + high)/2
    tries += 1
    
print(f"Square root of {n} is {guess}, in {tries} tries")

#--------------------------------------------------