| [e48510a] | 1 | /******************************************************************************
|
|---|
| 2 | * FILE: ser_prime.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This program generates primes. The approach taken is a "brute force"
|
|---|
| 5 | * method which requires increasingly greater amounts of cpu as the problem
|
|---|
| 6 | * size increases. It should lend itself well to an embarassingly parallel
|
|---|
| 7 | * solution since each prime can be computed independently of all other
|
|---|
| 8 | * primes.
|
|---|
| 9 | * AUTHOR: Blaise Barney 11/25/95 - adapted from version contributed by
|
|---|
| 10 | * Richard Ng & Wong Sze Cheong during MHPCC Singapore Workshop (8/22/95).
|
|---|
| 11 | * LAST REVISED: 04/12/05
|
|---|
| 12 | ****************************************************************************/
|
|---|
| 13 | #include <stdio.h>
|
|---|
| 14 | #include <stdlib.h>
|
|---|
| 15 | #include <math.h>
|
|---|
| 16 |
|
|---|
| 17 | #define LIMIT 2500000 /* Increase this to find more primes */
|
|---|
| 18 | #define PRINT 100000 /* Print a line after this many numbers */
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | int isprime(int n) {
|
|---|
| 22 | int i,squareroot;
|
|---|
| 23 | if (n>10) {
|
|---|
| 24 | squareroot = (int) sqrt(n);
|
|---|
| 25 | for (i=3; i<=squareroot; i=i+2)
|
|---|
| 26 | if ((n%i)==0)
|
|---|
| 27 | return 0;
|
|---|
| 28 | return 1;
|
|---|
| 29 | }
|
|---|
| 30 | /* Assume first four primes are counted elsewhere. Forget everything else */
|
|---|
| 31 | else
|
|---|
| 32 | return 0;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | int main(int argc, char *argv[])
|
|---|
| 37 | {
|
|---|
| 38 | int n, /* loop variables */
|
|---|
| 39 | pc, /* prime counter */
|
|---|
| 40 | foundone; /* most recent prime found */
|
|---|
| 41 |
|
|---|
| 42 | printf("Starting. Numbers to be scanned= %d\n",LIMIT);
|
|---|
| 43 |
|
|---|
| 44 | pc=4; /* Assume the primes less than 10 (2,3,5,7) are counted here */
|
|---|
| 45 |
|
|---|
| 46 | for (n=11; n<=LIMIT; n=n+2) {
|
|---|
| 47 | if (isprime(n)) {
|
|---|
| 48 | pc++;
|
|---|
| 49 | foundone = n;
|
|---|
| 50 | /***** Optional: print each prime as it is found
|
|---|
| 51 | printf("%d\n",foundone);
|
|---|
| 52 | *****/
|
|---|
| 53 | }
|
|---|
| 54 | if ( (n-1)%PRINT == 0 )
|
|---|
| 55 | printf("Numbers scanned= %d Primes found= %d\n",n-1,pc);
|
|---|
| 56 | }
|
|---|
| 57 | printf("Done. Largest prime is %d Total primes %d\n",foundone,pc);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|