0

I'm having some trouble with my code. I'm trying to write a method which will output all prime numbers from 2-10,000. I'm still a beginner in java and I wasn't sure how to go about doing this, I know I would use a binary search method and loops to do this. I tried to follow examples that I was reading through in my textbook and online; this is what I came up with, however it is not working properly. I'm not sure if it's entirely correct. Any help or advice on how to go about doing this or fixing this would be appreciated.

public static void prime() {
    int i;   // variable for loop

    for(i=2; i<=10000; i++)
    {
        int factors =0;
        int j = 1;

        while(j<=i)
        {
            if(i%j == 0)
            { 
                factors++;
            } //End if
            j++;
        } 

        if(factors == 2)
        {
            System.out.println(i);
        } //End if
    }// End for
} // End method prime
sstan
  • 35,425
  • 6
  • 48
  • 66
user4051397
  • 11
  • 1
  • 4
  • 1
    What makes you say it doesn't work properly. Give more details please. – sstan Jul 11 '15 at 02:12
  • See similar, answered question [here][1]. [1]: http://stackoverflow.com/questions/2831192/help-with-java-program-for-prime-numbers?rq=1 – spb1994 Jul 11 '15 at 02:12
  • 1
    "however it is not working properly" - it is not the most efficient way - but the algorithm is correct and the results are good. Why do you think it's not working? – Nir Alfasi Jul 11 '15 at 02:13
  • You *can* improve performance by checking only numbers up to sqrt(i). Replace the line of the `while` with `while(j<=Math.sqrt(i))` and `if(factors == 2)` with `if(factors == 1)`. – Nir Alfasi Jul 11 '15 at 02:16
  • The problem is nothing happens, nothing shows up in the console when I run my code. I only posted the portion of my code that I was having an issue with. This method is outside of my main method. The code in my main method works as it should and is outputted in the console. – user4051397 Jul 11 '15 at 02:24
  • @OP: Maybe you need to post a more complete program then. Because when I copy paste the `prime()` method you posted in your question, and call it from a `main()`, I do get results in the console. – sstan Jul 11 '15 at 02:27
  • 1
    I realized I did not call the method from main, which caused the error. When I made the change and called the method from main, it works as it should. Thank you for the help! – user4051397 Jul 11 '15 at 02:34

2 Answers2

1

Think about what prime means: a number that is only divisible by 1 and itself. Using that definition, you can write a much more efficient piece of code that checks whether the current number is evenly divisible by a number other than 1 and itself, i.e 2.

blasko
  • 690
  • 4
  • 8
0

One improvement that you can do is that if a number has than more than two factors stop testing if it is a prime number before reaching the limit of the second loop

public static void printPrimes()
{
    int i;   
    boolean isPrime ;

    for(i=2; i<=10000; i++)
    {
        int factors ;
        int j ;

        factors = 1 ;
        isPrime = true ;

        for(j = 2; isPrime && j <= i; j++)
        {
            if(i%j == 0)
            { 
                factors++;
            } 

            if(factors > 2)
            {
                isPrime = false ;
            }
        } 

        if(isPrime)
        {
            System.out.println(i);
        } 
    }

} 

if you want to use break or the isPrime flag as mention in this code here is a very good answer explaining why both approaches can be used and is more a matter of style avoiding complex look conditions

Is it bad practice to use break to exit a loop in java?

Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • @StephenC and the source for that is ? I like my code to be clear and avoid breaks if a simple flag does the trick – Mauricio Gracia Gutierrez Jul 11 '15 at 02:16
  • 1
    @MauricioGracia there is no "source" to write elegant code. A simple `break` makes the code flow easier to follow since you don't have to go back and forth to see what should the code do next when `isPrime` is set to false. – Nir Alfasi Jul 11 '15 at 02:20
  • What is read is "if it is a prime number print it". And elegant code is very subjective – Mauricio Gracia Gutierrez Jul 11 '15 at 02:22
  • http://stackoverflow.com/questions/18188123/is-it-bad-practice-to-use-break-to-exit-a-loop-in-java – Mauricio Gracia Gutierrez Jul 11 '15 at 02:24
  • Yes, it is subjective ... but I think if you asked 100 professional programmers, 95 or more would say that a `break` gives a more concise and elegant solution here. (Obviously, I have no definitive source for that. But my experience from looking at Java, etc code written by hundreds / thousands of other programmers in various open source projects bears this out.) – Stephen C Jul 11 '15 at 03:10
  • Also look (objectively) at the opinions expressed in the answers to the question you linked to. Don't you agree that they mostly say that using a `break` is usually clearer? Don't the voting patterns support that opinion? – Stephen C Jul 11 '15 at 03:18
  • @StephenC feel free to post your answer using your own coding style. The question of wheter or not to use break was closed since it it was mainly opinion based – Mauricio Gracia Gutierrez Jul 11 '15 at 04:42
  • @StephenC I know that breaks are very common but is just something that I dislike – Mauricio Gracia Gutierrez Jul 11 '15 at 04:42
  • @StephenC I did not notice that It was closed. – Mauricio Gracia Gutierrez Jul 11 '15 at 11:42