Fraction reduction: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 289: Line 289:
2988 have 9's omitted</pre>
2988 have 9's omitted</pre>


=={{header|C++}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|Kotlin}}
<lang cpp>#include <array>
<lang csharp>using System;
#include <iomanip>
#include <iostream>
#include <vector>


namespace FractionReduction {
int indexOf(const std::vector<int> &haystack, int needle) {
class Program {
auto it = haystack.cbegin();
static int IndexOf(int n, int[] s) {
auto end = haystack.cend();
int idx = 0;
for (int i = 0; i < s.Length; i++) {
for (; it != end; it = std::next(it)) {
if (s[i] == n) {
if (*it == needle) {
return i;
return idx;
}
}
return -1;
}
}
idx++;
}
return -1;
}


bool getDigits(int n, int le, std::vector<int> &digits) {
static bool GetDigits(int n, int le, int[] digits) {
while (n > 0) {
while (n > 0) {
auto r = n % 10;
var r = n % 10;
if (r == 0 || indexOf(digits, r) >= 0) {
if (r == 0 || IndexOf(r, digits) >= 0) {
return false;
return false;
}
le--;
digits[le] = r;
n /= 10;
}
return true;
}
}
le--;
digits[le] = r;
n /= 10;
}
return true;
}


int removeDigit(const std::vector<int> &digits, int le, int idx) {
static int RemoveDigit(int[] digits, int le, int idx) {
static std::array<int, 5> pows = { 1, 10, 100, 1000, 10000 };
int[] pows = { 1, 10, 100, 1000, 10000 };


int sum = 0;
var sum = 0;
auto pow = pows[le - 2];
var pow = pows[le - 2];
for (int i = 0; i < le; i++) {
for (int i = 0; i < le; i++) {
if (i == idx) continue;
if (i == idx) continue;
sum += digits[i] * pow;
sum += digits[i] * pow;
pow /= 10;
pow /= 10;
}
return sum;
}


}
int main() {
return sum;
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++) {
static void Main() {
var lims = new int[,] { { 12, 97 }, { 123, 986 }, { 1234, 9875 }, { 12345, 98764 } };
std::vector<int> nDigits(i + 2);
var count = new int[5];
std::vector<int> dDigits(i + 2);
var omitted = new int[5, 10];

for (int n = lims[i].first; n <= lims[i].second; n++) {
var upperBound = lims.GetLength(0);
for (int i = 0; i < upperBound; i++) {
std::fill(nDigits.begin(), nDigits.end(), 0);
bool nOk = getDigits(n, i + 2, nDigits);
var nDigits = new int[i + 2];
if (!nOk) {
var dDigits = new int[i + 2];
continue;
var blank = new int[i + 2];
for (int n = lims[i, 0]; n <= lims[i, 1]; n++) {
}
for (int d = n + 1; d <= lims[i].second + 1; d++) {
blank.CopyTo(nDigits, 0);
std::fill(dDigits.begin(), dDigits.end(), 0);
var nOk = GetDigits(n, i + 2, nDigits);
bool dOk = getDigits(d, i + 2, dDigits);
if (!nOk) {
if (!dOk) {
continue;
continue;
}
}
for (int d = n + 1; d <= lims[i, 1] + 1; d++) {
for (size_t nix = 0; nix < nDigits.size(); nix++) {
blank.CopyTo(dDigits, 0);
auto digit = nDigits[nix];
var dOk = GetDigits(d, i + 2, dDigits);
auto dix = indexOf(dDigits, digit);
if (!dOk) {
if (dix >= 0) {
continue;
auto rn = removeDigit(nDigits, i + 2, nix);
}
auto rd = removeDigit(dDigits, i + 2, dix);
for (int nix = 0; nix < nDigits.Length; nix++) {
if ((double)n / d == (double)rn / rd) {
var digit = nDigits[nix];
count[i]++;
var dix = IndexOf(digit, dDigits);
omitted[i][digit]++;
if (dix >= 0) {
if (count[i] <= 12) {
var rn = RemoveDigit(nDigits, i + 2, nix);
std::cout << n << '/' << d << " = " << rn << '/' << rd << " by omitting " << digit << "'s\n";
var rd = RemoveDigit(dDigits, i + 2, dix);
if ((double)n / d == (double)rn / rd) {
count[i]++;
omitted[i, digit]++;
if (count[i] <= 12) {
Console.WriteLine("{0}/{1} = {2}/{3} by omitting {4}'s", n, d, rn, rd, digit);
}
}
}
}
}
}
}
}
}
}
Console.WriteLine();
}
}
}


std::cout << '\n';
for (int i = 2; i <= 5; i++) {
Console.WriteLine("There are {0} {1}-digit fractions of which:", count[i - 2], i);
}
for (int j = 1; j <= 9; j++) {

for (int i = 2; i <= 5; i++) {
if (omitted[i - 2, j] == 0) {
std::cout << "There are " << count[i - 2] << ' ' << i << "-digit fractions of which:\n";
continue;
for (int j = 1; j <= 9; j++) {
}
if (omitted[i - 2][j] == 0) {
Console.WriteLine("{0,6} have {1}'s omitted", omitted[i - 2, j], j);
continue;
}
Console.WriteLine();
}
}
std::cout << std::setw(6) << omitted[i - 2][j] << " have " << j << "'s omitted\n";
}
}
std::cout << '\n';
}
}

return 0;
}</lang>
}</lang>
{{out}}
{{out}}
Line 477: Line 465:
2988 have 9's omitted</pre>
2988 have 9's omitted</pre>


=={{header|C#|C sharp}}==
=={{header|C++}}==
{{trans|Kotlin}}
{{trans|D}}
<lang csharp>using System;
<lang cpp>#include <array>
#include <iomanip>
#include <iostream>
#include <vector>


int indexOf(const std::vector<int> &haystack, int needle) {
namespace FractionReduction {
auto it = haystack.cbegin();
class Program {
auto end = haystack.cend();
static int IndexOf(int n, int[] s) {
for (int i = 0; i < s.Length; i++) {
int idx = 0;
if (s[i] == n) {
for (; it != end; it = std::next(it)) {
return i;
if (*it == needle) {
}
return idx;
}
return -1;
}
}
idx++;
}
return -1;
}


static bool GetDigits(int n, int le, int[] digits) {
bool getDigits(int n, int le, std::vector<int> &digits) {
while (n > 0) {
while (n > 0) {
var r = n % 10;
auto r = n % 10;
if (r == 0 || IndexOf(r, digits) >= 0) {
if (r == 0 || indexOf(digits, r) >= 0) {
return false;
return false;
}
le--;
digits[le] = r;
n /= 10;
}
return true;
}
}
le--;
digits[le] = r;
n /= 10;
}
return true;
}


static int RemoveDigit(int[] digits, int le, int idx) {
int removeDigit(const std::vector<int> &digits, int le, int idx) {
int[] pows = { 1, 10, 100, 1000, 10000 };
static std::array<int, 5> pows = { 1, 10, 100, 1000, 10000 };


var sum = 0;
int sum = 0;
var pow = pows[le - 2];
auto pow = pows[le - 2];
for (int i = 0; i < le; i++) {
for (int i = 0; i < le; i++) {
if (i == idx) continue;
if (i == idx) continue;
sum += digits[i] * pow;
sum += digits[i] * pow;
pow /= 10;
pow /= 10;
}
return sum;
}


int main() {
}
std::vector<std::pair<int, int>> lims = { {12, 97}, {123, 986}, {1234, 9875}, {12345, 98764} };
return sum;
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);
}
}
);


static void Main() {
for (size_t i = 0; i < lims.size(); i++) {
std::vector<int> nDigits(i + 2);
var lims = new int[,] { { 12, 97 }, { 123, 986 }, { 1234, 9875 }, { 12345, 98764 } };
std::vector<int> dDigits(i + 2);
var count = new int[5];

var omitted = new int[5, 10];
var upperBound = lims.GetLength(0);
for (int n = lims[i].first; n <= lims[i].second; n++) {
std::fill(nDigits.begin(), nDigits.end(), 0);
for (int i = 0; i < upperBound; i++) {
var nDigits = new int[i + 2];
bool nOk = getDigits(n, i + 2, nDigits);
var dDigits = new int[i + 2];
if (!nOk) {
var blank = new int[i + 2];
continue;
}
for (int n = lims[i, 0]; n <= lims[i, 1]; n++) {
blank.CopyTo(nDigits, 0);
for (int d = n + 1; d <= lims[i].second + 1; d++) {
var nOk = GetDigits(n, i + 2, nDigits);
std::fill(dDigits.begin(), dDigits.end(), 0);
if (!nOk) {
bool dOk = getDigits(d, i + 2, dDigits);
continue;
if (!dOk) {
}
continue;
for (int d = n + 1; d <= lims[i, 1] + 1; d++) {
}
blank.CopyTo(dDigits, 0);
for (size_t nix = 0; nix < nDigits.size(); nix++) {
var dOk = GetDigits(d, i + 2, dDigits);
auto digit = nDigits[nix];
if (!dOk) {
auto dix = indexOf(dDigits, digit);
continue;
if (dix >= 0) {
}
auto rn = removeDigit(nDigits, i + 2, nix);
for (int nix = 0; nix < nDigits.Length; nix++) {
auto rd = removeDigit(dDigits, i + 2, dix);
var digit = nDigits[nix];
if ((double)n / d == (double)rn / rd) {
var dix = IndexOf(digit, dDigits);
count[i]++;
if (dix >= 0) {
omitted[i][digit]++;
var rn = RemoveDigit(nDigits, i + 2, nix);
if (count[i] <= 12) {
var rd = RemoveDigit(dDigits, i + 2, dix);
std::cout << n << '/' << d << " = " << rn << '/' << rd << " by omitting " << digit << "'s\n";
if ((double)n / d == (double)rn / rd) {
count[i]++;
omitted[i, digit]++;
if (count[i] <= 12) {
Console.WriteLine("{0}/{1} = {2}/{3} by omitting {4}'s", n, d, rn, rd, digit);
}
}
}
}
}
}
}
}
}
}
Console.WriteLine();
}
}
}


for (int i = 2; i <= 5; i++) {
std::cout << '\n';
}
Console.WriteLine("There are {0} {1}-digit fractions of which:", count[i - 2], i);

for (int j = 1; j <= 9; j++) {
if (omitted[i - 2, j] == 0) {
for (int i = 2; i <= 5; i++) {
continue;
std::cout << "There are " << count[i - 2] << ' ' << i << "-digit fractions of which:\n";
}
for (int j = 1; j <= 9; j++) {
Console.WriteLine("{0,6} have {1}'s omitted", omitted[i - 2, j], j);
if (omitted[i - 2][j] == 0) {
}
continue;
Console.WriteLine();
}
}
std::cout << std::setw(6) << omitted[i - 2][j] << " have " << j << "'s omitted\n";
}
}
std::cout << '\n';
}
}

return 0;
}</lang>
}</lang>
{{out}}
{{out}}
Line 1,423: Line 1,423:


</pre>
</pre>

=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Combinatorics
<lang julia>using Combinatorics
Line 1,734: Line 1,735:
351 have 8's omitted
351 have 8's omitted
2988 have 9's omitted</pre>
2988 have 9's omitted</pre>

=={{header|MiniZinc}}==
===The Model===
<lang MiniZinc>
%Fraction Reduction. Nigel Galloway, September 5th., 2019
include "alldifferent.mzn"; include "member.mzn";
int: S;
array [1..9] of int: Pn=[1,10,100,1000,10000,100000,1000000,10000000,100000000];
array [1..S] of var 1..9: Nz; constraint alldifferent(Nz);
array [1..S] of var 1..9: Gz; constraint alldifferent(Gz);
var int: n; constraint n=sum(n in 1..S)(Nz[n]*Pn[n]);
var int: i; constraint i=sum(n in 1..S)(Gz[n]*Pn[n]); constraint n<i; constraint n*g=i*e;
var int: g; constraint g=sum(n in 1..S)(if n=a then 0 elseif n>a then Gz[n]*Pn[n-1] else Gz[n]*Pn[n] endif);
var int: e; constraint e=sum(n in 1..S)(if n=l then 0 elseif n>l then Nz[n]*Pn[n-1] else Nz[n]*Pn[n] endif);
var 1..S: l; constraint Nz[l]=w;
var 1..S: a; constraint Gz[a]=w;
var 1..9: w; constraint member(Nz,w) /\ member(Gz,w);

output [show(n)++"/"++show(i)++" becomes "++show(e)++"/"++show(g)++" when "++show(w)++" is omitted"]
</lang>
===The Tasks===
;Displaying 12 solutions
;minizinc --num-solutions 12 -DS=2
{{out}}
<pre>
16/64 becomes 1/4 when 6 is omitted
----------
26/65 becomes 2/5 when 6 is omitted
----------
19/95 becomes 1/5 when 9 is omitted
----------
49/98 becomes 4/8 when 9 is omitted
----------
==========
</pre>
;minizinc --num-solutions 12 -DS=3
{{out}}
<pre>
132/231 becomes 12/21 when 3 is omitted
----------
134/536 becomes 14/56 when 3 is omitted
----------
134/938 becomes 14/98 when 3 is omitted
----------
136/238 becomes 16/28 when 3 is omitted
----------
138/345 becomes 18/45 when 3 is omitted
----------
139/695 becomes 13/65 when 9 is omitted
----------
143/341 becomes 13/31 when 4 is omitted
----------
146/365 becomes 14/35 when 6 is omitted
----------
149/298 becomes 14/28 when 9 is omitted
----------
149/596 becomes 14/56 when 9 is omitted
----------
149/894 becomes 14/84 when 9 is omitted
----------
154/253 becomes 14/23 when 5 is omitted
----------
</pre>
;minizinc --num-solutions 12 -DS=4
{{out}}
<pre>
2147/3164 becomes 247/364 when 1 is omitted
----------
2314/3916 becomes 234/396 when 1 is omitted
----------
2147/5198 becomes 247/598 when 1 is omitted
----------
3164/5198 becomes 364/598 when 1 is omitted
----------
2314/6319 becomes 234/639 when 1 is omitted
----------
3916/6319 becomes 396/639 when 1 is omitted
----------
5129/7136 becomes 529/736 when 1 is omitted
----------
3129/7152 becomes 329/752 when 1 is omitted
----------
4913/7514 becomes 493/754 when 1 is omitted
----------
7168/8176 becomes 768/876 when 1 is omitted
----------
5129/9143 becomes 529/943 when 1 is omitted
----------
7136/9143 becomes 736/943 when 1 is omitted
----------
</pre>
;minizinc --num-solutions 12 -DS=5
{{out}}
<pre>
21356/31472 becomes 2356/3472 when 1 is omitted
----------
21394/31528 becomes 2394/3528 when 1 is omitted
----------
21546/31752 becomes 2546/3752 when 1 is omitted
----------
21679/31948 becomes 2679/3948 when 1 is omitted
----------
21698/31976 becomes 2698/3976 when 1 is omitted
----------
25714/34615 becomes 2574/3465 when 1 is omitted
----------
27615/34716 becomes 2765/3476 when 1 is omitted
----------
25917/34719 becomes 2597/3479 when 1 is omitted
----------
25916/36518 becomes 2596/3658 when 1 is omitted
----------
31276/41329 becomes 3276/4329 when 1 is omitted
----------
21375/41625 becomes 2375/4625 when 1 is omitted
----------
31584/41736 becomes 3584/4736 when 1 is omitted
----------
</pre>
;minizinc --num-solutions 12 -DS=6
{{out}}
<pre>
123495/172893 becomes 12345/17283 when 9 is omitted
----------
123594/164792 becomes 12354/16472 when 9 is omitted
----------
123654/163758 becomes 12654/16758 when 3 is omitted
----------
124678/135679 becomes 12478/13579 when 6 is omitted
----------
124768/164872 becomes 12768/16872 when 4 is omitted
----------
125349/149352 becomes 12549/14952 when 3 is omitted
----------
125394/146293 becomes 12534/14623 when 9 is omitted
----------
125937/127936 becomes 12537/12736 when 9 is omitted
----------
125694/167592 becomes 12564/16752 when 9 is omitted
----------
125769/135786 becomes 12769/13786 when 5 is omitted
----------
125769/165837 becomes 12769/16837 when 5 is omitted
----------
125934/146923 becomes 12534/14623 when 9 is omitted
----------
</pre>
;Count number of solutions
;minizinc --all-solutions -s -DS=3
{{out}}
<pre>
%%%mzn-stat: nSolutions=122
</pre>
;minizinc --all-solutions -s -DS=4
{{out}}
<pre>
%%%mzn-stat: nSolutions=660
</pre>
;minizinc --all-solutions -s -DS=5
{{out}}
<pre>
%%%mzn-stat: nSolutions=5087
</pre>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 2,141: Line 2,305:
Took 1m38.85577279s
Took 1m38.85577279s
*/</pre>
*/</pre>

=={{header|Python}}==
<lang python>def indexOf(haystack, needle):
idx = 0
for straw in haystack:
if straw == needle:
return idx
else:
idx += 1
return -1

def getDigits(n, le, digits):
while n > 0:
r = n % 10
if r == 0 or indexOf(digits, r) >= 0:
return False
le -= 1
digits[le] = r
n = int(n / 10)
return True

def removeDigit(digits, le, idx):
pows = [1, 10, 100, 1000, 10000]
sum = 0
pow = pows[le - 2]
i = 0
while i < le:
if i == idx:
i += 1
continue
sum = sum + digits[i] * pow
pow = int(pow / 10)
i += 1
return sum

def main():
lims = [ [ 12, 97 ], [ 123, 986 ], [ 1234, 9875 ], [ 12345, 98764 ] ]
count = [0 for i in range(5)]
omitted = [[0 for i in range(10)] for j in range(5)]

i = 0
while i < len(lims):
n = lims[i][0]
while n < lims[i][1]:
nDigits = [0 for k in range(i + 2)]
nOk = getDigits(n, i + 2, nDigits)
if not nOk:
n += 1
continue
d = n + 1
while d <= lims[i][1] + 1:
dDigits = [0 for k in range(i + 2)]
dOk = getDigits(d, i + 2, dDigits)
if not dOk:
d += 1
continue
nix = 0
while nix < len(nDigits):
digit = nDigits[nix]
dix = indexOf(dDigits, digit)
if dix >= 0:
rn = removeDigit(nDigits, i + 2, nix)
rd = removeDigit(dDigits, i + 2, dix)
if (1.0 * n / d) == (1.0 * rn / rd):
count[i] += 1
omitted[i][digit] += 1
if count[i] <= 12:
print "%d/%d = %d/%d by omitting %d's" % (n, d, rn, rd, digit)
nix += 1
d += 1
n += 1
print
i += 1

i = 2
while i <= 5:
print "There are %d %d-digit fractions of which:" % (count[i - 2], i)
j = 1
while j <= 9:
if omitted[i - 2][j] == 0:
j += 1
continue
print "%6s have %d's omitted" % (omitted[i - 2][j], j)
j += 1
print
i += 1
return None

main()</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|MiniZinc}}==
===The Model===
<lang MiniZinc>
%Fraction Reduction. Nigel Galloway, September 5th., 2019
include "alldifferent.mzn"; include "member.mzn";
int: S;
array [1..9] of int: Pn=[1,10,100,1000,10000,100000,1000000,10000000,100000000];
array [1..S] of var 1..9: Nz; constraint alldifferent(Nz);
array [1..S] of var 1..9: Gz; constraint alldifferent(Gz);
var int: n; constraint n=sum(n in 1..S)(Nz[n]*Pn[n]);
var int: i; constraint i=sum(n in 1..S)(Gz[n]*Pn[n]); constraint n<i; constraint n*g=i*e;
var int: g; constraint g=sum(n in 1..S)(if n=a then 0 elseif n>a then Gz[n]*Pn[n-1] else Gz[n]*Pn[n] endif);
var int: e; constraint e=sum(n in 1..S)(if n=l then 0 elseif n>l then Nz[n]*Pn[n-1] else Nz[n]*Pn[n] endif);
var 1..S: l; constraint Nz[l]=w;
var 1..S: a; constraint Gz[a]=w;
var 1..9: w; constraint member(Nz,w) /\ member(Gz,w);

output [show(n)++"/"++show(i)++" becomes "++show(e)++"/"++show(g)++" when "++show(w)++" is omitted"]
</lang>
===The Tasks===
;Displaying 12 solutions
;minizinc --num-solutions 12 -DS=2
{{out}}
<pre>
16/64 becomes 1/4 when 6 is omitted
----------
26/65 becomes 2/5 when 6 is omitted
----------
19/95 becomes 1/5 when 9 is omitted
----------
49/98 becomes 4/8 when 9 is omitted
----------
==========
</pre>
;minizinc --num-solutions 12 -DS=3
{{out}}
<pre>
132/231 becomes 12/21 when 3 is omitted
----------
134/536 becomes 14/56 when 3 is omitted
----------
134/938 becomes 14/98 when 3 is omitted
----------
136/238 becomes 16/28 when 3 is omitted
----------
138/345 becomes 18/45 when 3 is omitted
----------
139/695 becomes 13/65 when 9 is omitted
----------
143/341 becomes 13/31 when 4 is omitted
----------
146/365 becomes 14/35 when 6 is omitted
----------
149/298 becomes 14/28 when 9 is omitted
----------
149/596 becomes 14/56 when 9 is omitted
----------
149/894 becomes 14/84 when 9 is omitted
----------
154/253 becomes 14/23 when 5 is omitted
----------
</pre>
;minizinc --num-solutions 12 -DS=4
{{out}}
<pre>
2147/3164 becomes 247/364 when 1 is omitted
----------
2314/3916 becomes 234/396 when 1 is omitted
----------
2147/5198 becomes 247/598 when 1 is omitted
----------
3164/5198 becomes 364/598 when 1 is omitted
----------
2314/6319 becomes 234/639 when 1 is omitted
----------
3916/6319 becomes 396/639 when 1 is omitted
----------
5129/7136 becomes 529/736 when 1 is omitted
----------
3129/7152 becomes 329/752 when 1 is omitted
----------
4913/7514 becomes 493/754 when 1 is omitted
----------
7168/8176 becomes 768/876 when 1 is omitted
----------
5129/9143 becomes 529/943 when 1 is omitted
----------
7136/9143 becomes 736/943 when 1 is omitted
----------
</pre>
;minizinc --num-solutions 12 -DS=5
{{out}}
<pre>
21356/31472 becomes 2356/3472 when 1 is omitted
----------
21394/31528 becomes 2394/3528 when 1 is omitted
----------
21546/31752 becomes 2546/3752 when 1 is omitted
----------
21679/31948 becomes 2679/3948 when 1 is omitted
----------
21698/31976 becomes 2698/3976 when 1 is omitted
----------
25714/34615 becomes 2574/3465 when 1 is omitted
----------
27615/34716 becomes 2765/3476 when 1 is omitted
----------
25917/34719 becomes 2597/3479 when 1 is omitted
----------
25916/36518 becomes 2596/3658 when 1 is omitted
----------
31276/41329 becomes 3276/4329 when 1 is omitted
----------
21375/41625 becomes 2375/4625 when 1 is omitted
----------
31584/41736 becomes 3584/4736 when 1 is omitted
----------
</pre>
;minizinc --num-solutions 12 -DS=6
{{out}}
<pre>
123495/172893 becomes 12345/17283 when 9 is omitted
----------
123594/164792 becomes 12354/16472 when 9 is omitted
----------
123654/163758 becomes 12654/16758 when 3 is omitted
----------
124678/135679 becomes 12478/13579 when 6 is omitted
----------
124768/164872 becomes 12768/16872 when 4 is omitted
----------
125349/149352 becomes 12549/14952 when 3 is omitted
----------
125394/146293 becomes 12534/14623 when 9 is omitted
----------
125937/127936 becomes 12537/12736 when 9 is omitted
----------
125694/167592 becomes 12564/16752 when 9 is omitted
----------
125769/135786 becomes 12769/13786 when 5 is omitted
----------
125769/165837 becomes 12769/16837 when 5 is omitted
----------
125934/146923 becomes 12534/14623 when 9 is omitted
----------
</pre>
;Count number of solutions
;minizinc --all-solutions -s -DS=3
{{out}}
<pre>
%%%mzn-stat: nSolutions=122
</pre>
;minizinc --all-solutions -s -DS=4
{{out}}
<pre>
%%%mzn-stat: nSolutions=660
</pre>
;minizinc --all-solutions -s -DS=5
{{out}}
<pre>
%%%mzn-stat: nSolutions=5087
</pre>


=={{header|Perl}}==
=={{header|Perl}}==
Line 2,571: Line 2,404:
1298/3894 => 128/384 removed 9
1298/3894 => 128/384 removed 9
1298/5192 => 128/512 removed 9</pre>
1298/5192 => 128/512 removed 9</pre>

=={{header|Perl 6}}==
{{works with|Rakudo|2019.07.1}}
;[[wp:Anomalous cancellation|Anomalous Cancellation]]
<lang perl6>my %reduced;
my $digits = 2..4;

for $digits.map: * - 1 -> $exp {
my $start = sum (0..$exp).map( { 10 ** $_ * ($exp - $_ + 1) });
my $end = 10**($exp+1) - sum (^$exp).map( { 10 ** $_ * ($exp - $_) } ) - 1;

($start ..^ $end).race(:8degree, :3batch).map: -> $den {
next if $den.contains: '0';
next if $den.comb.unique <= $exp;

for $start ..^ $den -> $num {
next if $num.contains: '0';
next if $num.comb.unique <= $exp;

my $set = ($den.comb.head(* - 1).Set ∩ $num.comb.skip(1).Set);
next if $set.elems < 1;

for $set.keys {
my $ne = $num.trans: $_ => '', :delete;
my $de = $den.trans: $_ => '', :delete;
if $ne / $de == $num / $den {
print "\b" x 40, "$num/$den:$_ => $ne/$de";
%reduced{"$num/$den:$_"} = "$ne/$de";
}
}
}
}


print "\b" x 40, ' ' x 40, "\b" x 40;

my $digit = $exp +1;
my %d = %reduced.pairs.grep: { .key.chars == ($digit * 2 + 3) };
say "\n({+%d}) $digit digit reduceable fractions:";
for 1..9 {
my $cnt = +%d.pairs.grep( *.key.contains: ":$_" );
next unless $cnt;
say " $cnt with removed $_";
}
say "\n 12 Random (or all, if less) $digit digit reduceable fractions:";
say " {.key.substr(0, $digit * 2 + 1)} => {.value} removed {.key.substr(* - 1)}"
for %d.pairs.pick(12).sort;
}</lang>
{{out|Sample output}}
<pre>(4) 2 digit reduceable fractions:
2 with removed 6
2 with removed 9

12 Random (or all, if less) 2 digit reduceable fractions:
16/64 => 1/4 removed 6
19/95 => 1/5 removed 9
26/65 => 2/5 removed 6
49/98 => 4/8 removed 9

(122) 3 digit reduceable fractions:
9 with removed 3
1 with removed 4
6 with removed 5
15 with removed 6
16 with removed 7
15 with removed 8
60 with removed 9

12 Random (or all, if less) 3 digit reduceable fractions:
149/298 => 14/28 removed 9
154/352 => 14/32 removed 5
165/264 => 15/24 removed 6
176/275 => 16/25 removed 7
187/286 => 17/26 removed 8
194/291 => 14/21 removed 9
286/385 => 26/35 removed 8
286/682 => 26/62 removed 8
374/572 => 34/52 removed 7
473/572 => 43/52 removed 7
492/984 => 42/84 removed 9
594/693 => 54/63 removed 9

(660) 4 digit reduceable fractions:
14 with removed 1
25 with removed 2
92 with removed 3
14 with removed 4
29 with removed 5
63 with removed 6
16 with removed 7
17 with removed 8
390 with removed 9

12 Random (or all, if less) 4 digit reduceable fractions:
1348/4381 => 148/481 removed 3
1598/3196 => 158/316 removed 9
1783/7132 => 178/712 removed 3
1978/5934 => 178/534 removed 9
2971/5942 => 271/542 removed 9
2974/5948 => 274/548 removed 9
3584/4592 => 384/492 removed 5
3791/5798 => 391/598 removed 7
3968/7936 => 368/736 removed 9
4329/9324 => 429/924 removed 3
4936/9872 => 436/872 removed 9
6327/8325 => 627/825 removed 3</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,824: Line 2,551:
"10 minutes and 13s"
"10 minutes and 13s"
</pre>
</pre>

=={{header|Python}}==
<lang python>def indexOf(haystack, needle):
idx = 0
for straw in haystack:
if straw == needle:
return idx
else:
idx += 1
return -1

def getDigits(n, le, digits):
while n > 0:
r = n % 10
if r == 0 or indexOf(digits, r) >= 0:
return False
le -= 1
digits[le] = r
n = int(n / 10)
return True

def removeDigit(digits, le, idx):
pows = [1, 10, 100, 1000, 10000]
sum = 0
pow = pows[le - 2]
i = 0
while i < le:
if i == idx:
i += 1
continue
sum = sum + digits[i] * pow
pow = int(pow / 10)
i += 1
return sum

def main():
lims = [ [ 12, 97 ], [ 123, 986 ], [ 1234, 9875 ], [ 12345, 98764 ] ]
count = [0 for i in range(5)]
omitted = [[0 for i in range(10)] for j in range(5)]

i = 0
while i < len(lims):
n = lims[i][0]
while n < lims[i][1]:
nDigits = [0 for k in range(i + 2)]
nOk = getDigits(n, i + 2, nDigits)
if not nOk:
n += 1
continue
d = n + 1
while d <= lims[i][1] + 1:
dDigits = [0 for k in range(i + 2)]
dOk = getDigits(d, i + 2, dDigits)
if not dOk:
d += 1
continue
nix = 0
while nix < len(nDigits):
digit = nDigits[nix]
dix = indexOf(dDigits, digit)
if dix >= 0:
rn = removeDigit(nDigits, i + 2, nix)
rd = removeDigit(dDigits, i + 2, dix)
if (1.0 * n / d) == (1.0 * rn / rd):
count[i] += 1
omitted[i][digit] += 1
if count[i] <= 12:
print "%d/%d = %d/%d by omitting %d's" % (n, d, rn, rd, digit)
nix += 1
d += 1
n += 1
print
i += 1

i = 2
while i <= 5:
print "There are %d %d-digit fractions of which:" % (count[i - 2], i)
j = 1
while j <= 9:
if omitted[i - 2][j] == 0:
j += 1
continue
print "%6s have %d's omitted" % (omitted[i - 2][j], j)
j += 1
print
i += 1
return None

main()</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|Racket}}==
=={{header|Racket}}==
Line 2,971: Line 2,866:
The digit 9 was crossed out 2988 times
The digit 9 was crossed out 2988 times
</pre>
</pre>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.07.1}}
;[[wp:Anomalous cancellation|Anomalous Cancellation]]
<lang perl6>my %reduced;
my $digits = 2..4;

for $digits.map: * - 1 -> $exp {
my $start = sum (0..$exp).map( { 10 ** $_ * ($exp - $_ + 1) });
my $end = 10**($exp+1) - sum (^$exp).map( { 10 ** $_ * ($exp - $_) } ) - 1;

($start ..^ $end).race(:8degree, :3batch).map: -> $den {
next if $den.contains: '0';
next if $den.comb.unique <= $exp;

for $start ..^ $den -> $num {
next if $num.contains: '0';
next if $num.comb.unique <= $exp;

my $set = ($den.comb.head(* - 1).Set ∩ $num.comb.skip(1).Set);
next if $set.elems < 1;

for $set.keys {
my $ne = $num.trans: $_ => '', :delete;
my $de = $den.trans: $_ => '', :delete;
if $ne / $de == $num / $den {
print "\b" x 40, "$num/$den:$_ => $ne/$de";
%reduced{"$num/$den:$_"} = "$ne/$de";
}
}
}
}


print "\b" x 40, ' ' x 40, "\b" x 40;

my $digit = $exp +1;
my %d = %reduced.pairs.grep: { .key.chars == ($digit * 2 + 3) };
say "\n({+%d}) $digit digit reduceable fractions:";
for 1..9 {
my $cnt = +%d.pairs.grep( *.key.contains: ":$_" );
next unless $cnt;
say " $cnt with removed $_";
}
say "\n 12 Random (or all, if less) $digit digit reduceable fractions:";
say " {.key.substr(0, $digit * 2 + 1)} => {.value} removed {.key.substr(* - 1)}"
for %d.pairs.pick(12).sort;
}</lang>
{{out|Sample output}}
<pre>(4) 2 digit reduceable fractions:
2 with removed 6
2 with removed 9

12 Random (or all, if less) 2 digit reduceable fractions:
16/64 => 1/4 removed 6
19/95 => 1/5 removed 9
26/65 => 2/5 removed 6
49/98 => 4/8 removed 9

(122) 3 digit reduceable fractions:
9 with removed 3
1 with removed 4
6 with removed 5
15 with removed 6
16 with removed 7
15 with removed 8
60 with removed 9

12 Random (or all, if less) 3 digit reduceable fractions:
149/298 => 14/28 removed 9
154/352 => 14/32 removed 5
165/264 => 15/24 removed 6
176/275 => 16/25 removed 7
187/286 => 17/26 removed 8
194/291 => 14/21 removed 9
286/385 => 26/35 removed 8
286/682 => 26/62 removed 8
374/572 => 34/52 removed 7
473/572 => 43/52 removed 7
492/984 => 42/84 removed 9
594/693 => 54/63 removed 9

(660) 4 digit reduceable fractions:
14 with removed 1
25 with removed 2
92 with removed 3
14 with removed 4
29 with removed 5
63 with removed 6
16 with removed 7
17 with removed 8
390 with removed 9

12 Random (or all, if less) 4 digit reduceable fractions:
1348/4381 => 148/481 removed 3
1598/3196 => 158/316 removed 9
1783/7132 => 178/712 removed 3
1978/5934 => 178/534 removed 9
2971/5942 => 271/542 removed 9
2974/5948 => 274/548 removed 9
3584/4592 => 384/492 removed 5
3791/5798 => 391/598 removed 7
3968/7936 => 368/736 removed 9
4329/9324 => 429/924 removed 3
4936/9872 => 436/872 removed 9
6327/8325 => 627/825 removed 3</pre>


=={{header|REXX}}==
=={{header|REXX}}==