|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Python课老师出的题 实在是不会
There is a mathematical puzzle of the form 厂x + 厂y = 厂n. where x, y and n are all
positive integers. For example, if n = 8, x = 2 and y = 2 and if n = 666, x = 74 and y = 296. Given
n, the puzzle is to find the pair x and y, under the assumption that x<= y. It is possible to solve this
mathematically, just like solving for f(x) = 3x3-11x2+9x-2=0. However, we are here to use the
computer to systematically look for the answer for us, just like our way of solving for an equation
by searching for the root within an interval a and b. There is no answer for many values of n, e.g.
n = 17.
(a) Given an input of value of n, write the pseudo-code that will return a pair of values of x and y (x <= y) satisfying the puzzle 厂x + 厂y = 厂n.
Explain briefly your approach.
(b) Your pseudo-code may run fast to solve for n = 2009, with a possible pair x = 41 and y = 1476
(maybe x = 164, y = 1025 or x = 369 and y = 656). However, it is already running very slowly for
n = 2020923, and even much slower for n = 100151002. Redesign your pseudo-code without
solving the puzzle mathematically to make it run faster. In other words, try to search more
efficiently. You can assume the existence of a function iszero(v) that will return True if the
value v is technically zero (very close to zero) and False otherwise. Explain briefly how you
achieve the improvement in efficiency, i.e. running faster.
来个大佬解答一下吧 我快枯了。 |
|