summaryrefslogtreecommitdiff
path: root/087.c
blob: e1b1ad8640aed5cc33398adf7908398adbfe191e (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
#include "common.h"
#include <stdlib.h>
#include <string.h>

const unsigned long limit = 50000000;

int main(void)
{
	unsigned long* p = primes(8000);
	unsigned long count = p[0];
	unsigned long i2, i3, i4, counter;
	char* numbers = malloc(limit*sizeof(char));
	memset(numbers, 0, limit);

	for(i2=1; i2<=count; i2++)
		for(i3=1; i3<=count; i3++)
			for(i4=1; i4<=count; i4++)
			{
				unsigned long p4 = p[i4];
				unsigned long p3 = p[i3];
				unsigned long p2 = p[i2];
				unsigned long long result = (unsigned long long)p4*p4*p4*p4;
				if(result >= limit) break;
				result += (unsigned long long)p3*p3*p3;
				if(result >= limit) break;
				result += (unsigned long long)p2*p2;
				if(result >= limit) break;

				numbers[result] = 1;
			}

	counter = 0;
	for(i2=0; i2<limit; i2++)
		if(numbers[i2] == 1)
			counter++;

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

	free(p);
	return 0;
}