1.) ARGUMENT OF CORRECTNESS FOR CODE 2.) RUNTIME ANALYSIS FOR CODE CODE: \'\'\'r
ID: 3749039 • Letter: 1
Question
1.) ARGUMENT OF CORRECTNESS FOR CODE
2.) RUNTIME ANALYSIS FOR CODE
CODE:
'''returns the largest integer x such that x^k does not exceed n,
assuming k and n are both positive integers'''
def iroot(k, n):
num=1
#looping until num^k is greater than n
while True:
#checking if current number ^ k is greater than n
if pow(num,k)>n:
#returning previous number
return num-1
#moving to next number
num+=1
#testing various cases
print(iroot(3,125)) #5
print(iroot(3,126)) #5
print(iroot(3,124)) #4
Explanation / Answer
in first function passing the value 3,125
initialize the num value to 1
and while will looping until false so the there is no statement of returning false, when the is conditon is true it will return.
in if condition it will check 1^3>n and 1^3=1 is grater than n value means condition will return false 1>125 the condition will false and if condition will not execute till the the num vaue will increase num=2
in second time the same process will execute and the num vaue will increase to num=3
in third time the same process will execute and the num vaue will increase to num=4
in fourth time the same process will execute and the num vaue will increase to num=5
in fifth time the same process will execute and the num vaue will increase to num=6
in sixth time in if conditon the 6^3=216 it will check 216>n means 216>125 yes then the if conditon will execute and the decrement the num value to 1 num=5 so it will return the 5
the same process will continue to second print statement and it will print 5
the same process will continue to third print statement and it will print 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.