aboutsummaryrefslogtreecommitdiff
path: root/challenge-012/duncan-c-white/C/mkprimes.c
blob: a72491bf2e2f7fbc821eef25ff637bbbba398a19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>


// creates primes.[ch]

int main( int argc, char **argv )
{
	int n = 5001;		// find all primes up to 5001 by default..

	if( argc == 1 )
	{
		n = 5001;
	} else if( argc == 2 )
	{
		n = atoi( argv[1] );
	} else
	{
		fprintf( stderr, "Usage: mkprimes [MAXVALUE]\n" );
		exit(1);
	}

	int *isprime = (int *) malloc( (n+1) * sizeof(int) );
	assert( isprime != NULL );

	int i;
	for( i=1; i<=n; i++ )
	{
		isprime[i] = 1;		// inititally
	}

	int upper = (int)(sqrt((double)(n)));
	//printf( "debug: n=%d, upper=%d\n", n, upper );
	for( i=2; i<=upper; i++ )
	{
		if( isprime[i] )
		{
			//printf( "debug: crossing out multiples of %d\n", i );
			int j;
			for( j=i*i; j<=n; j+=i )
			{
				isprime[j] = 0;
			}
		}
	}
	int np=0;
	FILE *out = fopen( "primes.pl", "w" );
	assert( out != NULL );

	fprintf( out, "# array of all primes <= %d (nb: 1 is not a prime)\n\n", n );
	fprintf( out, "my @prime = (" );
	for( i=2; i<=n; i++ )
	{
		if( isprime[i] )	// finally
		{
			if( np % 10 == 0 )
			{
				fprintf( out, "\n\t" );
			}
			fprintf( out, "%d, ", i );
			np++;
		}
	}
	fprintf( out, "\n);\n\n" );
	fprintf( out, "my $NPRIMES = %d;\n", np );

	fclose( out );
}