0

so my assignment is to create a java program that prints all numbers from 2-500 inclusively, with 10 numbers in each line. I've learned the basics of loops and a brief intro to methods. I've looked at other questions/answers, but, I can't seem to combine all the info into workable code.

int n;
for (int count = 0; count < 10; count++) {
    for (n=2; n <= 500; n++){
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i != 0)
                System.out.print(i + " ");
        }
    }
System.out.println();    
}

This is all I have and I'm printing out a really weird set of numbers. I know there's something wrong with my if statement and I know I haven't set it up to properly print out the right values, I'm just at a complete loss.

Honestly, I'm still having trouble figuring out why I only need to check i up to the sqrt of n.

Basically I want the line of code to somehow print out

2 3 5 7 11 13 17 19 23 29 and then jump to the next line where it will continue

etc etc etc

Is using a method the most efficient way? I was trying to avoid it because methods was only recently introduced.

Maciej Dobrowolski
  • 10,083
  • 3
  • 40
  • 58
  • 2
    Why are you printing `i`? Surely you want to print `n`? And then, only if all the divisibility tests fail. – Dawood ibn Kareem Feb 19 '14 at 00:01
  • I've posted [an answer](http://stackoverflow.com/a/21884207/849891) to this question under [its duplicate as marked here](http://stackoverflow.com/questions/18667400/printing-out-prime-numbers-from-2-to-1000). – Will Ness Feb 20 '14 at 07:01

2 Answers2

1

As mentioned in the comment, your question has been already answered. All that remains is: how to print 10 numbers per row, or, more exactly, how to jump to the next row each time you print ten numbers.

Here is an idea:

int c = 0;  // This will hold the number of prints
for(int n = 2; n <= 500; n++) {
    // Check if n is prime (already answered), and if it is, print it
    if(isPrime(n) {
        System.out.print(n + " ");
        c++;
    }
    if(c >= 10) {
        System.out.print("\n");
        c = 0;
    }
}
Barranka
  • 18,876
  • 13
  • 56
  • 79
  • Okay, so I've pretty much implemented everything but the break line after 10 outputs. So basically, the `i` in the main method gets transferred over into the `n` in the isPrime method where it then gets compared to another `i` variable? – user3278117 Feb 19 '14 at 00:35
0

Something like this:

int n;
for (n = 2; n <= 500; n++) {
    double limit = Math.sqrt(n);
    int i;
    for (i = 2; i <= limit; i++) {
        if (n % i == 0) {
            break;
        }
    }
    if (i > limit) {
        System.out.print(n + " ");
    }
}

Output is:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
Ashot Karakhanyan
  • 2,734
  • 3
  • 21
  • 28