summaryrefslogtreecommitdiff
path: root/062.c
blob: 4ba3350d8157f13e59d89fd69b2c6533dfc2b2e6 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct number
{
	char digits[10];
};

const int cubecount = 30000;
struct number* cubedigits;

int main(void)
{
	int i, j;

	cubedigits = (struct number*) malloc(cubecount*sizeof(struct number));
	memset(cubedigits, 0, cubecount*sizeof(struct number));

	for(i=1; i<cubecount; i++)
	{
		unsigned long long c = (unsigned long long)i*i*i;
		int smallest = 0;
		char count;

		while(c > 0)
		{
			char digit = c % 10;
			cubedigits[i].digits[digit]++;
			c /= 10;
		}

		// search for permutations (i.e. same digits)
		count = 0;
		for(j=1; j<i; j++)
		{
			if(memcmp(cubedigits[i].digits, cubedigits[j].digits, sizeof(struct number)) == 0)
			{
				if(smallest == 0)
					smallest = j;

				count++;
			}
		}

		if(count == 4) // 4 additional permutations
		{
			printf("%lli\n", (unsigned long long)smallest*smallest*smallest);
			break;
		}
	}

	free(cubedigits);

	return 0;
}