Repository navigation
Expand file tree
/
Copy pathsolve.c
More file actions
78 lines (69 loc) · 1.68 KB
/
Copy pathsolve.c
File metadata and controls
78 lines (69 loc) · 1.68 KB
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
72
73
74
75
76
77
78
#include <stdlib.h>
#include "data.h"
#include "convert.h"
void print_as_words(unsigned int v)
{
char tmp[max_words_length];
number_to_words(tmp,v);
printf("%s", tmp);
}
static unsigned int icount;
static unsigned long sum, ccount;
inline static int target_reached(const unsigned long count)
{
return count >= 51000000000L;
}
inline static void yield(unsigned int number, unsigned int length)
{
++icount;
sum += number;
ccount += length;
if (target_reached(ccount)) {
printf("Final Integer: %u\n", number);
printf("Written as: "); print_as_words(number); printf("\n");
printf("Integer Sum: %lu\n", sum);
printf("Character Count: %lu\n", ccount);
printf("Number of integers: %u\n", icount);
exit(0);
}
}
inline static void thousands(unsigned int number, unsigned int length)
{
for (int i=0; i<BASE; ++i)
yield(number + base[i].num, length + base[i].len);
}
inline static void millions(unsigned int number, unsigned int length)
{
// Don't iterate this million if we can just account for it in bulk
unsigned long fast = 44872000L + length * 999999L;
if (!target_reached(ccount + fast)) {
ccount += fast;
icount += 999999;
sum += 499999500000L;
sum += 999999L * (unsigned long) number;
return;
}
for (int i=0; i<MIDDLE; ++i) {
unsigned int mn = middle[i].num;
unsigned int tn = number + mn, tl = length + middle[i].len;
yield(tn, tl);
if (mn > 999)
thousands(tn,tl);
}
}
inline static void iterate_sorted_word_numbers()
{
for (int i=0; i<HIGH; ++i) {
unsigned int hn = high[i].num, hl = high[i].len;
yield(hn, hl);
if (hn > 999999)
millions(hn,hl);
else if (hn > 999)
thousands(hn,hl);
}
}
int main()
{
iterate_sorted_word_numbers();
return 1;
}