Fraction reduction: Difference between revisions
Content deleted Content added
promoted draft task to a (full) task. |
|||
Line 205: | Line 205: | ||
printf("\n"); |
printf("\n"); |
||
} |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
<pre>16/64 = 1/4 by omitting 6's |
|||
19/95 = 1/5 by omitting 9's |
|||
26/65 = 2/5 by omitting 6's |
|||
49/98 = 4/8 by omitting 9's |
|||
132/231 = 12/21 by omitting 3's |
|||
134/536 = 14/56 by omitting 3's |
|||
134/938 = 14/98 by omitting 3's |
|||
136/238 = 16/28 by omitting 3's |
|||
138/345 = 18/45 by omitting 3's |
|||
139/695 = 13/65 by omitting 9's |
|||
143/341 = 13/31 by omitting 4's |
|||
146/365 = 14/35 by omitting 6's |
|||
149/298 = 14/28 by omitting 9's |
|||
149/596 = 14/56 by omitting 9's |
|||
149/894 = 14/84 by omitting 9's |
|||
154/253 = 14/23 by omitting 5's |
|||
1234/4936 = 124/496 by omitting 3's |
|||
1239/6195 = 123/615 by omitting 9's |
|||
1246/3649 = 126/369 by omitting 4's |
|||
1249/2498 = 124/248 by omitting 9's |
|||
1259/6295 = 125/625 by omitting 9's |
|||
1279/6395 = 127/635 by omitting 9's |
|||
1283/5132 = 128/512 by omitting 3's |
|||
1297/2594 = 127/254 by omitting 9's |
|||
1297/3891 = 127/381 by omitting 9's |
|||
1298/2596 = 128/256 by omitting 9's |
|||
1298/3894 = 128/384 by omitting 9's |
|||
1298/5192 = 128/512 by omitting 9's |
|||
12349/24698 = 1234/2468 by omitting 9's |
|||
12356/67958 = 1236/6798 by omitting 5's |
|||
12358/14362 = 1258/1462 by omitting 3's |
|||
12358/15364 = 1258/1564 by omitting 3's |
|||
12358/17368 = 1258/1768 by omitting 3's |
|||
12358/19372 = 1258/1972 by omitting 3's |
|||
12358/21376 = 1258/2176 by omitting 3's |
|||
12358/25384 = 1258/2584 by omitting 3's |
|||
12359/61795 = 1235/6175 by omitting 9's |
|||
12364/32596 = 1364/3596 by omitting 2's |
|||
12379/61895 = 1237/6185 by omitting 9's |
|||
12386/32654 = 1386/3654 by omitting 2's |
|||
There are 4 2-digit fractions of which: |
|||
2 have 6's omitted |
|||
2 have 9's omitted |
|||
There are 122 3-digit fractions of which: |
|||
9 have 3's omitted |
|||
1 have 4's omitted |
|||
6 have 5's omitted |
|||
15 have 6's omitted |
|||
16 have 7's omitted |
|||
15 have 8's omitted |
|||
60 have 9's omitted |
|||
There are 660 4-digit fractions of which: |
|||
14 have 1's omitted |
|||
25 have 2's omitted |
|||
92 have 3's omitted |
|||
14 have 4's omitted |
|||
29 have 5's omitted |
|||
63 have 6's omitted |
|||
16 have 7's omitted |
|||
17 have 8's omitted |
|||
390 have 9's omitted |
|||
There are 5087 5-digit fractions of which: |
|||
75 have 1's omitted |
|||
40 have 2's omitted |
|||
376 have 3's omitted |
|||
78 have 4's omitted |
|||
209 have 5's omitted |
|||
379 have 6's omitted |
|||
591 have 7's omitted |
|||
351 have 8's omitted |
|||
2988 have 9's omitted</pre> |
|||
=={{header|C++}}== |
|||
{{trans|D}} |
|||
<lang cpp>#include <array> |
|||
#include <iomanip> |
|||
#include <iostream> |
|||
#include <vector> |
|||
int indexOf(const std::vector<int> &haystack, int needle) { |
|||
auto it = haystack.cbegin(); |
|||
auto end = haystack.cend(); |
|||
int idx = 0; |
|||
for (; it != end; it = std::next(it)) { |
|||
if (*it == needle) { |
|||
return idx; |
|||
} |
|||
idx++; |
|||
} |
|||
return -1; |
|||
} |
|||
bool getDigits(int n, int le, std::vector<int> &digits) { |
|||
while (n > 0) { |
|||
auto r = n % 10; |
|||
if (r == 0 || indexOf(digits, r) >= 0) { |
|||
return false; |
|||
} |
|||
le--; |
|||
digits[le] = r; |
|||
n /= 10; |
|||
} |
|||
return true; |
|||
} |
|||
int removeDigit(const std::vector<int> &digits, int le, int idx) { |
|||
static std::array<int, 5> pows = { 1, 10, 100, 1000, 10000 }; |
|||
int sum = 0; |
|||
auto pow = pows[le - 2]; |
|||
for (int i = 0; i < le; i++) { |
|||
if (i == idx) continue; |
|||
sum += digits[i] * pow; |
|||
pow /= 10; |
|||
} |
|||
return sum; |
|||
} |
|||
int main() { |
|||
std::vector<std::pair<int, int>> lims = { {12, 97}, {123, 986}, {1234, 9875}, {12345, 98764} }; |
|||
std::array<int, 5> count; |
|||
std::array<std::array<int, 10>, 5> omitted; |
|||
std::fill(count.begin(), count.end(), 0); |
|||
std::for_each(omitted.begin(), omitted.end(), |
|||
[](auto &a) { |
|||
std::fill(a.begin(), a.end(), 0); |
|||
} |
|||
); |
|||
for (size_t i = 0; i < lims.size(); i++) { |
|||
std::vector<int> nDigits(i + 2); |
|||
std::vector<int> dDigits(i + 2); |
|||
for (int n = lims[i].first; n <= lims[i].second; n++) { |
|||
std::fill(nDigits.begin(), nDigits.end(), 0); |
|||
bool nOk = getDigits(n, i + 2, nDigits); |
|||
if (!nOk) { |
|||
continue; |
|||
} |
|||
for (int d = n + 1; d <= lims[i].second + 1; d++) { |
|||
std::fill(dDigits.begin(), dDigits.end(), 0); |
|||
bool dOk = getDigits(d, i + 2, dDigits); |
|||
if (!dOk) { |
|||
continue; |
|||
} |
|||
for (size_t nix = 0; nix < nDigits.size(); nix++) { |
|||
auto digit = nDigits[nix]; |
|||
auto dix = indexOf(dDigits, digit); |
|||
if (dix >= 0) { |
|||
auto rn = removeDigit(nDigits, i + 2, nix); |
|||
auto rd = removeDigit(dDigits, i + 2, dix); |
|||
if ((double)n / d == (double)rn / rd) { |
|||
count[i]++; |
|||
omitted[i][digit]++; |
|||
if (count[i] <= 12) { |
|||
std::cout << n << '/' << d << " = " << rn << '/' << rd << " by omitting " << digit << "'s\n"; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
std::cout << '\n'; |
|||
} |
|||
for (int i = 2; i <= 5; i++) { |
|||
std::cout << "There are " << count[i - 2] << ' ' << i << "-digit fractions of which:\n"; |
|||
for (int j = 1; j <= 9; j++) { |
|||
if (omitted[i - 2][j] == 0) { |
|||
continue; |
|||
} |
|||
std::cout << std::setw(6) << omitted[i - 2][j] << " have " << j << "'s omitted\n"; |
|||
} |
|||
std::cout << '\n'; |
|||
} |
} |
||