I want to make the following program in java, but I don't know.please tell me。

Asked 1 years ago, Updated 1 years ago, 459 views

Consider (x,y,n) satisfying 1/x+1/y=1/n,x yy with x,y,n as a positive integer.For example, when n=4, there are three different solutions (x, y, 4).Concretely, there are three of 1/5+1/20=1/4→(x,y,n)=(5,20,4)1/6+1/12=1/4→(x,y,n)=(6,12,4)1/8+1/8=1/4→(x,y,n)=(8,4).Create a program to find the smallest n with more than 200 solutions and find its value.

I've written this far myself, but I don't know any more.Please tell me where and how to fix it.

public class Main {
    public static void main(String[]args) arrows Exception {
      int n = 1;
      int temp = 0;
      while(true){
        while(temp<=n){
         intx = new java.util.Random().nextInt(n)+1;
         inty = new java.util.Random().nextInt(n)+1;
         int sum = 0;
          temp = 0;
         if(1/x+1/y==1/temp&&x<=y){
           sum = sum+1;
          if(sum>200){
            break;
          }
         temp++;
       }
       n++;
      }
    }
    System.out.println(n);
  }
}

error:

Main.java:21:error:unreachable statement
    System.out.println(n);
    ^
1 error

java

2022-11-11 23:25

1 Answers

First of all, there are three things that are wrong.

First of all, the reason for the error is that System.out.println, which is next to while(true), cannot be unreachable.
The only break you have is to go through one loop outside, so
while(temp<=n){, but not the outer while(true){
So I got an error because I scratched the loop next to the loop that never comes out.
You can label the outer loop and multiply it by break label name to get out of the outer loop

The int type division is truncated
if(1/x+1/y==1/temp is 0 on both sides if x,y,temp is 2 or more

It may be strange to use random numbers to solve problems with fixed answers. If it's a random number, the same combination of xy will appear, so
If you have one solution, you just repeat the combination until 200 times.
So you must loop all the different xy combinations and count sum.

But actually, if you turn the loop by x, y can be calculated if x and n are determined.
Find y=xn/(x-n) from the formula 1/x+1/y=1/n
You can check if this y is an integer and y>=x

public class Main {
  public static void main(String[]args) arrows Exception {
    int n = 1;

    L:// Label to get out of the inside of multiple loops
    while(true){
      /*
        The lower limit of x is n+1 (1/n=1/x+1/y>1/x)  
        : To add up to 1/100, both must be 1/101 or less or one side will pass.

        The upper limit for x is 2n (1/n=1/x+1/y<=1/x+1/x=2/x) 
        : In order to add up to 1/100, one of them has to be 1/200+1/201 to reach 1/100.

        So x should loop from n+1 to 2n.
        If x<=2n, automatically y>=2n>=x(1/x+1/y>=1/2n+1/y=n)
        Therefore, even if y>=x is not checked, both xy-switched items such as 1/5+1/20=1/4/1/20+1/5=1/4 will not be counted.
      */
      int sum = 0;
      for(intx=n+1;x<=n*2;x++){
        inty=x*n/(x-n);
        if(x*n%(x-n)==0){//xn/(x-n) remaining is 0=>integer
          sum = sum+1;
          if(sum>200)
            break L;// If it is not labeled, only the inner loop can be pulled out, so attach the outer loop label and pull it out.

          // Debugging only when n = 4
          if(n==4)System.out.println("1/"+x+"+1/"+y+"=1/"+n+"(sum="+sum+")";
        }
      }
      n++;
    }
    System.out.println(n);
  }
}


2022-11-12 06:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.