-
Notifications
You must be signed in to change notification settings - Fork 0
/
032.c
56 lines (46 loc) · 1.34 KB
/
032.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
// Find the sum of all numbers that can be written as pandigital products.
#include <stdio.h>
#include <stdbool.h>
int main() {
void findPandigitals(const int a_min, const int a_max,
const int b_min, const int b_max,
const int prod_min, const int prod_max);
// 2-digit * 3-digit = 4-digit
findPandigitals(12, 81, 123, 833, 1000, 9999);
// 1-digit * 4-digit = 4-digit
findPandigitals(2, 8, 1234, 4315, 1000, 9999);
return 0;
}
// Find pandigital expressions a * b = c
void findPandigitals(const int a_min, const int a_max,
const int b_min, const int b_max,
const int prod_min, const int prod_max) {
bool isPandigital(char *arr);
char arr[10];
int prod;
for (int a = a_min; a <= a_max; a++) {
for (int b = b_min; b <= b_max; b++) {
prod = a * b;
if (prod >= prod_min && prod <= prod_max) {
sprintf(arr, "%i%i%i", a, b, prod);
if (isPandigital(arr)) {
printf("%i * %i = %i\n", a, b, prod);
}
}
}
}
}
// Checks whether sequence uses digits 1 thru 9 exactly once
bool isPandigital(char *arr) {
bool used[10] = {false};
int n;
while (*arr != '\0') {
n = *arr - '0';
if (used[n] == true || n == 0) {
return false;
}
used[n] = true;
arr++;
}
return true;
}