
Patrick B. answered 03/04/21
Math and computer tutor/teacher
#0 - 99
for count in range(100):
print (count)
iStart = 0
iStop = 99
count = iStart
while (int(count)<=int(iStop)):
print (count)
count = count+1
#--------------------------------------
#1- 100
for count in range(1,101):
print (count)
iStart = 1
iStop = 100
count = iStart
while (int(count)<=int(iStop)):
print (count)
count = count+1
#-------------------------------------
#100 , 99, 98, ..., 3, 2, 1
for count in range(100, 0, -1):
print (count)
iStart = 100
iStop = 1
count = iStart
while (int(count)>=int(iStop)):
print (count)
count = count-1
#----------- Factorial -----------------
def Factorial( n):
factorialReturn=1
iCount = n
while (iCount>1):
factorialReturn = int(factorialReturn)*int(iCount)
iCount = int(iCount)-1
return factorialReturn
#---------------------------
for iLoop in range(26):
nBang = Factorial(iLoop)
print(str(iLoop) + "! = " + str(nBang))
#--------- Log ---------------------
def IntegralLogBase2( n ):
X = n
iCount=-1
while (int(X)!=0):
X = int(X)//2
iCount = int(iCount)+1
return iCount
#-------------
for i in range(1,101):
log2 = IntegralLogBase2(i)
print(" integral log of " + str(i) + " base 2 is " + str(log2))
#--------- Binary Search ---------------------------------------------
def BinarySearch(intArrayA, intTargetInt):
iIndexPos = -1
n=len(intArrayA)
Left=0
Right=int(n)-1
if ( int(intArrayA[0])==int(intTargetInt)):
iIndexPos=0
elif (int(intArrayA[n-1])==int(intTargetInt)):
iIndexPos=n-1
else:
while abs(int(Left) -int(Right))>1:
mid = (int(Left) + int(Right))//2
print("left = " + str(Left) + ": Right = " + str(Right) + ": Mid = " + str(mid))
if int(intArrayA[mid]) < int(intTargetInt):
Left = int(mid)
elif int(intArrayA[mid]) > int(intTargetInt):
Right = int(mid)
else: #found it!!!!
iIndexPos = mid
break
return iIndexPos
#------------------------------------
A = []
for iLoop in range(1,101):
A.append(iLoop)
intTargetInt=13
indexReturn = BinarySearch(A,intTargetInt)
if (indexReturn>-1):
print("Target integer " + str(intTargetInt) +" found at index position " + str(indexReturn) + ". \n Here it is :>" + str(A[indexReturn]) + "<")
else:
print("Sorry, Target integer " + str(intTargetInt) + " not found")
#------------------------------------------------------
"""
(1) break statement exists out of the INNER most loop
(2) Infinite loop occurs when you forget to update the
loop counter
(3) This is the binary search algorithmn which is order O(log N)
so Log_2(M-N)
"""