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)
Answers
answer is 1 because s-s=1
Given:
def h(n):
s = 0
for i in range(1,n+1):
if n%i > 0:
s = s+1
return(s)
Answer:
h(41) - h(40) = 7
Therefore, 7 is the answer of h(41) - h(40)
Explanation:
h(41)
function h will be called and 41 will be passed to n
Loop will start from i = 1 and will go till i = 41
i if(n%i > 0) s=s+1
1 false 0
2 true 1
3 true 2
4 true 3
5 true 4
.
.
.
39 true 38
40 true 39
41 false 39
So, h(41) = 39
h(40)
function h will be called and 40 will be passed to n
Loop will start from i = 1 and will go till i = 40
i if(n%i > 0) s=s+1
1 false 0
2 false 0
3 true 1
4 false 1
5 false 1
6 true 2
7 true 3
8 false 3
9 true 4
10 false 4
11 true 5
.
.
.
19 true 13
20 false 13
21 true 14
.
.
.
39 true 32
40 false 32
So, h(40) = 32
Therefore, h(41) - h(40) = 39 - 32 = 7