Answer to Question #156617 in Python for shinde

Question #156617

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)
1
Expert's answer
2021-01-19T06:42:45-0500

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS