Skip to content

Commit 55433fb

Browse files
committed
faster primes
1 parent fc74dd8 commit 55433fb

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

prog.7.4.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
4+
// Modified program to generate prime numbers
5+
6+
int main (void)
7+
{
8+
int p, i, primes[50], primeIndex = 2;
9+
bool isPrime;
10+
11+
primes[0] = 2;
12+
primes[1] = 3;
13+
14+
for ( p = 5; p <= 50; p = p + 2 ) {
15+
isPrime = true;
16+
17+
for ( i = 1; isPrime && p / primes[i] >= primes[i]; ++i )
18+
if ( p % primes[i] == 0 )
19+
isPrime = false;
20+
21+
if ( isPrime == true ) {
22+
primes[primeIndex] = p;
23+
++primeIndex;
24+
}
25+
}
26+
27+
for ( i = 0; i < primeIndex; ++i )
28+
printf ("%i ", primes[i]);
29+
30+
printf ("\n");
31+
32+
return 0;
33+
}

0 commit comments

Comments
 (0)