Home  >  Q&A  >  body text

python - 求这样的4个自然数p、q、r、s(p<=q<=r<=s),使得一下等式成立:1/p+1/q+1/r+1/s=1。

PHP中文网PHP中文网2741 days ago1193

reply all(4)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:29:36

    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)

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:29:36

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:29:36

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


    Questions I answered: Python-QA

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:29:36

    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.

    reply
    0
  • Cancelreply