Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - For what value of n would g(47,n) return 5?

i am new at Python. while i was doing exercises ,i just come across that question. First, how can i solve that. Second, what is role of res=0 and res=res+1?

For what value of n would g(47,n) return 5?

def g(m, n):
    res=0
    while m>=n:
        res=res+1
        m=m-n
    return res

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Mathematically, the integers should fall within the interval m/6 < n <= m/5, which gives use only 8 and 9 as feasible n values.

To verify the result, you can try

>>> g(47,7)  
6
>>> g(47,8)  
5
>>> g(47,9)    
5
>>> g(47,10)   
4

If you don't mind check all possible values, you can try

[[n, g(47,n)] for n in range(1,47)]

which gives

>>> [[n, g(47,n)] for n in range(1,47)]
[[1, 47], [2, 23], [3, 15], [4, 11], [5, 9], [6, 7], [7, 6], [8, 5], [9, 5], [10, 4], [11, 4], [12, 3], [13, 3], [14, 3], [15, 3], [16, 2], [17, 2], [18, 2], [19, 2], [20, 2], [21, 2], [22, 2], [23, 2], [24, 1], [25, 1], [26, 1], [27, 1], [28, 1], [29, 1], [30, 1], [31, 1], [32, 1], [33, 1], [34, 1], [35, 1], [36, 1], [37, 1], [38, 1], [39, 1], [40, 1], [41, 1], [42, 1], [43, 1], [44, 1], [45, 1], [46, 1]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...