summaryrefslogtreecommitdiff
path: root/134.c
blob: 3f6ea3212d855a8c210cf02f37932a9ae91e2123 (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
#include "common.h"
#include <stdlib.h>

// TODO: improve performance!  not yet working on 32 bit systems

unsigned int getmod(unsigned long p1)
{
	unsigned int mod = 1;

	if(p1 < 10)
		mod = 10;
	else if(p1 < 100)
		mod = 100;
	else if(p1 < 1000)
		mod = 1000;
	else if(p1 < 10000)
		mod = 10000;
	else if(p1 < 100000)
		mod = 100000;
	else if(p1 < 1000000)
		mod = 1000000;
	else
		mod = 0; // should not occur and therefore result in division by zero

	return mod;
}

unsigned long findn(unsigned long p1, unsigned long p2)
{
	unsigned int mod = getmod(p1);
	unsigned long n = mod + p1;
	while(n % p2 != 0)
		n += mod;

	return n;
}

int main(void)
{
	unsigned long* p = primes(1000020);
	unsigned long pcount = p[0];
	unsigned long p1, p2, pos;
	unsigned long long result = 0;

	for(pos=3; p[pos]<=1000000; pos++)
	{
		p1 = p[pos];
		p2 = p[pos+1];
		if(pos%1000==0)
			printf("p1: %li\n", p1);
		result += findn(p1, p2);
	}

	printf("%li\n", result);

	free(p);
	return 0;
}