What is h(41)-h(40), given the definition of h below?
def h(n):
s = 0
for i in range(1,n+1):
if n%i > 0:
s = s+1
return(s)
Hello. Answer is 7.
Also I want to give you some explanations what the function does. The function gets some argument n. Let's set n is 5 for example. Then the function creates a sequence of numbers from 1 to n with range(1, n+1) function (result will be 1, 2, 3, 4, 5 in our case) and iterates over each of this numbers. If remainder of dividing n to the number from the sequence greater then 0, variable s increments by 1. For our sequence we gets remainders 0, 1, 2, 1, 0 and variable s equal to 3. When the iteration complete, function returns s value.
So, if n is 41, the function returns 39 and when n is 40 the function returns 32. 39 - 32 = 7
Comments
Leave a comment