-
Notifications
You must be signed in to change notification settings - Fork 0
/
033.c
51 lines (40 loc) · 1.24 KB
/
033.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
// Discover all the fractions with an unorthodox cancelling method.
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isCurious(const int n, const int d);
for (int d = 11; d <= 99; d++) {
for (int n = 10; n < d; n++) {
if (n % 10 != 0 && d % 10 != 0) { // if non-trivial
isCurious(n, d);
}
}
}
return 0;
}
bool isCurious(const int n, const int d) {
double cancelDigit(char digits[], const char cancel, const int canceled_n);
char digits[5];
double canceled_value = 0;
sprintf(digits, "%i%i", n, d);
if (digits[0] == digits[2] || digits[0] == digits[3]) {
canceled_value = cancelDigit(digits, digits[0], digits[1] - '0');
} else if (digits[1] == digits[2] || digits[1] == digits[3]) {
canceled_value = cancelDigit(digits, digits[1], digits[0] - '0');
}
if ((double)n / d == canceled_value) {
printf("CURIOUS! %i / %i\n", n, d);
return true;
}
return false;
}
// Print the result of a digit cancel and return the quotient
double cancelDigit(char digits[], const char cancel, const int canceled_n) {
int canceled_d;
for (int i = 2; i < 4; i++) {
if (digits[i] != cancel) {
canceled_d = digits[i] - '0';
}
}
return (double)canceled_n / canceled_d;
}