I want to improve the execution time of the program that requires a solution that meets the requirements

Asked 1 years ago, Updated 1 years ago, 341 views

I would like to create a program that uses x,y,n as a positive integer to find the smallest number of n solutions that satisfy 1/x+1/y=1/n and x<=y, but even if I write the following program in paiza, it says time.out.How can I ask for answers without taking long?

If "a, b(a<=b) is taken as a positive integer and x=n+a, y=n+b is given as a hint, the conditional expression becomes n2=ab.Therefore, a, b are limited to the divisor of n の.

public class Main {
    public static void main(String[]args) arrows Exception {
        int n = 1;    
        while(true){
            int sum = 0;
            for(intx=n+1;x<=n*2;x++){
                inty=x*n/(x-n);
                if(Math.power(n,2)==x*y&&x<=y){
                    if(x*n%(x-n)==0){ 
                        sum = sum+1;
                        if(sum>1000){
                            break; 
                        }
                    }
                }
                n++;
            }
            System.out.println(n);
        }
    }
}

java

2023-01-25 12:59

1 Answers

This program does not meet the title prior to improved runtime.

First, you should check the behavior when n=1.


2023-01-25 14:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.