-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem32.c
102 lines (90 loc) · 2.04 KB
/
problem32.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
bool isPandigital(uint32_t num){
bool digits[10] = { false };
while(num){
uint8_t digit = num % 10;
// if a digit is previously read or if the digit is a 0 return false
if(digits[digit] || !digit)
return false;
// else set the array element and continue
digits[digit] = true;
num /= 10;
}
return true;
}
// function expects two numbers and return 1 if the two numbers and their product is
bool prodIsPandigital(uint32_t a, uint32_t b){
// a * b = c
uint32_t c = a * b;
bool digits[10] = { false };
for(uint32_t i = 0; i != 3; i++){
uint32_t num;
switch(i){
case 0:
num = a;
break;
case 1:
num = b;
break;
case 2:
num = c;
break;
}
while(num){
uint8_t digit = num % 10;
// if a digit is previously read or if the digit is a 0 return false
if(digits[digit] || !digit)
return false;
// else set the array element and continue
digits[digit] = true;
num /= 10;
}
}
for(uint8_t i = 1; i != sizeof(digits); i++)
if(!digits[i])
return false;
return true;
}
void insertifnotthere(uint32_t val, uint32_t* arr, size_t size){
for(uint32_t i = 0; i != size; i++){
if(arr[i] == val)
return;
if(!arr[i]){
arr[i] = val;
return;
}
}
}
uint32_t arrsum(uint32_t* arr, uint32_t size){
uint32_t sum = 0;
for(uint32_t i = 0; i != size; i++)
sum += arr[i];
return sum;
}
int main(void){
uint32_t i = 1, j;
uint32_t results[16] = {0};
while(i){
if(isPandigital(i)){
j = 1;
while(j < (10000000 / i)){
if(prodIsPandigital(i, j)){
if(i > j)
goto getout;
else
insertifnotthere(i * j, results, sizeof(results) / sizeof(results[0]));
}
j++;
}
}
i++;
}
getout:
; // ik it doesntmake sense but clang goes nuts without it
uint32_t sum = arrsum(results, sizeof(results) / sizeof(results[0]));
printf("the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital is %u\n", sum);
return EXIT_SUCCESS;
}