Calculate Prime Numbers

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Calculate Prime Numbers

This Java example program shows how to calculate prime numbers.

[edit] Java Source Code

  • Package: com.darwinsys
  • File: Primes.java
  1. package com.darwinsys;
  2.  
  3. /*
  4.  * Compute prime numbers, after Knuth, Vol 1, Sec 1.3.2, Alg. "P". Unlike Knuth, I don't build table
  5.  * formatting into computational programs; output is one per line. <p> Note that there may be more
  6.  * efficient algorithms for finding primes. Consult a good book on numerical algorithms.
  7.  * 
  8.  * @author Ian Darwin
  9.  */
  10. public class Primes {
  11.     /** The default stopping point for primes */
  12.     public static final long DEFAULT_STOP = 600L; //4294967295L;
  13.     /** The first prime number */
  14.     public static final int FP = 2;
  15.  
  16.     static int MAX = 10000;
  17.  
  18.     public static void main(String[] args) {
  19. 	long[] prime = new long[MAX];
  20.  
  21. 	long stop = DEFAULT_STOP;
  22. 	if (args.length == 1) {
  23. 	    stop = Long.parseLong(args[0]);
  24. 	}
  25.  
  26. 	prime[1] = FP; // P1 (ignore prime[0])
  27. 	long n = FP + 1; // odd candidates
  28. 	int j = 1; // numberFound
  29.  
  30. 	boolean isPrime = true; // for 3
  31.  
  32. 	do {
  33.  
  34. 	    if (isPrime) {
  35. 		if (j == MAX - 1) {
  36. 		    // Grow array dynamically if needed
  37. 		    long[] np = new long[MAX * 2];
  38. 		    System.arraycopy(prime, 0, np, 0, MAX);
  39. 		    MAX *= 2;
  40. 		    prime = np;
  41. 		}
  42. 		prime[++j] = n; // P2
  43. 		isPrime = false;
  44. 	    }
  45. 	    n += 2; // P4
  46.  
  47. 	    for (int k = 2; k <= j && k < MAX; k++) { // P5, P6, P8
  48. 		long q = n / prime[k];
  49. 		long r = n % prime[k];
  50. 		if (r == 0) {
  51. 		    break;
  52. 		}
  53. 		if (q <= prime[k]) { // P7
  54. 		    isPrime = true;
  55. 		    break;
  56. 		}
  57. 	    }
  58.  
  59. 	} while (n < stop); // P3
  60.  
  61. 	for (int i = 1; i <= j; i++)
  62. 	    System.out.println(prime[i]);
  63.     }
  64. }

[edit] What Result You Can Get

Run this example, you will get:

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
503
509
521
523
541
547
557
563
569
571
577
587
593
599

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools