Finding specific positive integer

user5161233

I want to find the integer, n, so that there are 1000 combinations when 1/z + 1/x = 1/n. This is my code:

int counter = 0;
    double n = 1;

    while (true) {
        for (double i = 1; i < 10000; i++) {
            for (double t = 1; t < 10000; t++) {
                if ((1/i) + (1/t) == (1/n)) {                       
                    counter++;
                    System.out.println(counter);
                }
            }           
        }   
        if (counter < 1000) {
            counter = 0;
            n++;
        } else {
            System.out.println("Counterr: " + counter);
            System.out.println("Answer: " + n);
            System.exit(0);
        } 
    } 

It works if I try to find 4 combinations, but not when 1000. Why?

Vasily Liaskovsky

That's probably because of floating point precision issues. You use increment ++ operator on double within a loop and compare doubles by ==. Result of those operations may be not as expected, especially after many iterations.

Try changing your code to work with integers with exact precision, not doubles. Note that your equation

1/z + 1/x = 1/n

is equivalent to

n * (x + z) = x * z

So, change your loop as follows:

    for (int i = 1; i < 10000; i++) {
        for (int t = 1; t < 10000; t++) {
            if (n * (i + t) == i * t) {
                counter++;
                System.out.println(counter);
            }
        }           
    }

Also, there is now more clear how to optimize your iteration ranges. Think about lower and upper limits for i and t - reducing iteration ranges will improve overall performance significantly.

Unfortunately, I didn't tested the result at the moment, but I'm pretty sure you should do calculations in integers, not in doubles.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Optimization with positive integer parameters

From Dev

Finding specific values in an array

From Dev

Finding the smallest integer

From Dev

DocumentFilter for negative and positive integer

From Dev

Finding an equation that results in a specific integer, Python

From Dev

Finding a single integer in an array

From Dev

Finding the minimum positive value

From Dev

Matching positive integer with haskell

From Dev

Finding number of positive numbers in list

From Dev

Finding a specific keyword in a paragraph

From Dev

Prime Factorization of a Positive Integer with Streams

From Dev

MongoDB have an integer always be positive

From Dev

Python: Finding nearest integer value to specific integer

From Dev

Finding number of positive numbers in list

From Dev

Python - Finding and multiplying an integer after specific word

From Dev

Minimal positive integer divisible by n

From Dev

Finding specific numbers in an array?

From Dev

finding a specific date in MySql

From Dev

Positive Integer in Velocity Template

From Dev

finding minimum number in a integer

From Dev

Finding a specific nibble in an integer

From Dev

Validating a string as a positive integer

From Dev

Finding start and end indices of positive curves in data

From Dev

Finding Maximum integer and minimum integer in a while loop

From Dev

finding a repeating integer in an array

From Dev

Test to see if input is a positive integer

From Dev

bash allow only positive integer or positive integer with decimal point

From Dev

Adding all digits for a positive integer

From Dev

Finding if an array of positive integers form a closed polygon