summaryrefslogtreecommitdiff
path: root/euler58.c
blob: ac4607349e142f67cad1302d05b59637b65b46c4 (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
#include <stdio.h>

/*
 *   delta = 4n+4           delta = 4n+2
 *                 5     3
 *                   [1]
 *                 7     9
 *   delta = 4n+6           delta = 4n+8
 */

int isprime(int n)
{
	if (n == 1 || n % 2 == 0)
		return 0;

	for (int i = 3; i * i <= n; i += 2) {
		if (n % i == 0)
			return 0;
	}

	return 1;
}

int main()
{
	int ndiag = 5;
	int nprime = 3;
	int s[4] = {3, 5, 7, 9};
	int n = 2;

	while (nprime * 10 > ndiag) {
		s[0] += n * 4 + 2;
		s[1] += n * 4 + 4;
		s[2] += n * 4 + 6;
		s[3] += n * 4 + 8;
		for (int i = 0; i < 4; i++) {
			if (isprime(s[i]))
				nprime++;
		}
		ndiag += 4;
		n += 2;
	}
	int sidelen = n + 1;
	printf("%d\n", sidelen);
}