简体   繁体   中英

Nested “FOR-loops” not working

I am doing an excercise in the book "Java how to program". The excercise wants me to write a method that determines if a number is "prime". (A "Prime number" is a positiv integer which is only dividable with itself and 1). Then I am supposed to implement the method in an application that displays all integers up to 10 000. I use "double-values" to test whether the remainder is 0 or not, to test dividability. Anyway, I just don´t get the program to work, it displays all numbers fro 3, with an increement on how many times each number is displayed (3 44 555 etc). Can anyone please tell me what I´m doing wrong?

The code is the following:

public class Oppgave625 
{

public static void main(String[] args) 
{
for(double a = 2; a <= 10000; a++)
    {
    for(double b = 1; b < a; b++)
        {
        if (prime(a, b) !=0)
            {
            System.out.printf("%.0f ", prime(a, b));
            }
        }

    }

}

static double prime(double x, double y)
{
if (x % y != 0)
    {
    return x;
    }
else
    {
    return 0;
    }
}

}

Use int instead. double is not good for this purpose

you might want to read this article to understand the use of the % Operator for floating point numbers.

It's great that you posted sample code for this, but there are several things that are wrong:

  • you should not use a floating point type for this, but an int or a long. Floating point types should never be used for precise values.

  • you are making two calls to your prime function, effectively doubling the required steps

  • your prime function only tells you whether two numbers divide themselves evenly, it does not tell you whether one is a prime or not

  • for prime numbers, you should use a more efficient algorithm instead of calculating the same values over and over for each number. Look up Sieve of Eratosthenes .

Actually, there were many individual errors in here. I shortened the prime() function to the point where it was only a modulo op, so I was able to inline it. Second, I inverted the test so it checked for numbers that do not have a remainder, and continues to the next number as soon as it finds a divisor. Third, I changed b = 1 so that we do not check for numbers divisible by 1, because this would result to all numbers. Finally, I only print out the numbers for which we do not discover a divisor. The final result:

public static void main(String[] args) {
    outer:
    for (int a = 2; a <= 1000; a++) {
        for (int b = 2; b < a; b++) {
            if (a % b == 0) {
                continue outer;
            }
        }
        System.out.println(a);
    }
}

Edit: I forgot to mention, I also changed the types from floats to ints, since I'm sure that's what you meant.

   boolean isPrime = true;
    for (int i = 2; i<=100; i++){
     for(int j = 2; j<=i/2; j++){
        isPrime = true;
         if (i%j==0){
           isPrime = false;
             break;
             }
            }
            if (isPrime){
                Log.d("PrimeNumber",""+i);
            }
        }

You are approaching the problem like this: The number A is NOT prime, whenever i can find a number B that can divide A without a remainder. Bur right now, you print out A whenever it is not dividable by B.

Instead you could say: whenever A not divisible by B, increase B. When i found a B to divide A, quit the inner loop, print nothing. When i found no B, print A and quit loop.

Furthermore, you only have to test for divisibility of A until (a/2)-1.

A prime number is a number that is only divisible by one and itself. That is: one number. Your code is comparing two numbers as in the Euclidean algorithm for testing coprime-ness . This is very different than testing if a number is prime .

Your code should look something like this:

for i = 2 to 10,000 {
    if( isPrime(i) ){
        print i
    }
}

function isPrime( int n ){
    for i = 2 to n {
        next if i == n
        if( n % i == 0 ){
            return 0;
        }
    }
    return 1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM