python - 求这样的4个自然数p、q、r、s(p<=q<=r<=s),使得一下等式成立:1/p+1/q+1/r+1/s=1。
PHP中文网
PHP中文网 2017-04-18 09:27:36
0
4
1187
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(4)
刘奇

Violent solution;
Integers are 10,000 times easier to use than floating point numbers

for p in range(1,100):
    for q in range(p,100):
        for r in range(q,100):
            for s in range(r,100):
                if p * q * r + p * q * s + p * r * s + q * r * s == p * q * r * s:
                    print(p,q,r,s)
Peter_Zhu

Violence, algorithmically it should only be violent.
But there are many ways to use violence.

  1. After multiplying both sides (pqrs), we get $$ qrs+prs+pqs+pqr=pqrs $$. After $$, we can brute force three of them and calculate the fourth one.

  2. Arrange the above formula and get $$ ps(q+r)+qr(p+s)=pqrs $$ $$ qr(p+s)=ps[qr-(q+r)] $$
    $$ frac{p+s}{ps}=frac{qr-(q+r)}{qr} $$

In this way, you only need to violence two of them, and write down the results during the violence, and check the table each time to see if the value calculated this time has appeared before.

左手右手慢动作

If you only want one solution, then p=q=r=s=4 is fine!


Questions I answered: Python-QA

小葫芦

I also think of violent solutions, but I have a new discovery.

1/p+1/q+1/r+1/s=1 and p<=q<=r<=s, we can get that p is the largest when p=q=r=s, 4/p>=1, Then p<=4, then the maximum value in the loop is determined. The maximum value of the for loop is 4, and values ​​larger than 4 do not need to be considered. In the same way, we can deduce q<=6, r<=12, s<=42, which can reduce the scope of the for loop.

It should be that every time x (x=1) on the right side of the equation is determined, a maximum and minimum value of p can be determined.
Every time p is determined (when looping), a maximum and minimum value of q can be determined.
The same goes for r and s.

However, I haven’t been able to express the rules of 4, 6, 12, and 42 with formulas. There should be some formula algorithm that can be applied to 1/p+1/q+1/r+1/s+1/. ..=x.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!