Fraction reduction: Difference between revisions
Content deleted Content added
Added Wren |
|||
Line 4,171: | Line 4,171: | ||
351 have 8's omitted |
351 have 8's omitted |
||
2988 have 9's omitted</pre> |
2988 have 9's omitted</pre> |
||
=={{header|Wren}}== |
|||
{{trans|Go}} |
|||
{{libheader|Wren-dynamic}} |
|||
{{libheader|Wren-fmt}} |
|||
A translation of Go's second version which is itself based on the Phix entry. |
|||
Have still needed to restrict to 5-digit fractions which finishes in just under 2 minutes on my machine. |
|||
<lang ecmascript>import "/dynamic" for Struct |
|||
import "/fmt" for Fmt |
|||
var Result = Struct.create("Result", ["n", "nine"]) |
|||
var toNumber = Fn.new { |digits, removeDigit| |
|||
var digits2 = digits.toList |
|||
if (removeDigit != 0) { |
|||
var d = digits2.indexOf(removeDigit) |
|||
digits2.removeAt(d) |
|||
} |
|||
var res = digits2[0] |
|||
var i = 1 |
|||
while (i < digits2.count) { |
|||
res = res * 10 + digits2[i] |
|||
i = i + 1 |
|||
} |
|||
return res |
|||
} |
|||
var nDigits = Fn.new { |n| |
|||
var res = [] |
|||
var digits = List.filled(n, 0) |
|||
var used = List.filled(9, false) |
|||
for (i in 0...n) { |
|||
digits[i] = i + 1 |
|||
used[i] = true |
|||
} |
|||
while (true) { |
|||
var nine = List.filled(9, 0) |
|||
for (i in 0...used.count) { |
|||
if (used[i]) nine[i] = toNumber.call(digits, i+1) |
|||
} |
|||
res.add(Result.new(toNumber.call(digits, 0), nine)) |
|||
var found = false |
|||
for (i in n-1..0) { |
|||
var d = digits[i] |
|||
if (!used[d-1]) { |
|||
Fiber.abort("something went wrong with 'used' array") |
|||
} |
|||
used[d-1] = false |
|||
var j = d |
|||
while (j < 9) { |
|||
if (!used[j]) { |
|||
used[j] = true |
|||
digits[i] = j + 1 |
|||
for (k in i + 1...n) { |
|||
digits[k] = used.indexOf(false) + 1 |
|||
used[digits[k]-1] = true |
|||
} |
|||
found = true |
|||
break |
|||
} |
|||
j = j + 1 |
|||
} |
|||
if (found) break |
|||
} |
|||
if (!found) break |
|||
} |
|||
return res |
|||
} |
|||
for (n in 2..5) { |
|||
var rs = nDigits.call(n) |
|||
var count = 0 |
|||
var omitted = List.filled(9, 0) |
|||
for (i in 0...rs.count-1) { |
|||
var xn = rs[i].n |
|||
var rn = rs[i].nine |
|||
for (j in i + 1...rs.count) { |
|||
var xd = rs[j].n |
|||
var rd = rs[j].nine |
|||
for (k in 0..8) { |
|||
var yn = rn[k] |
|||
var yd = rd[k] |
|||
if (yn != 0 && yd != 0 && xn/xd == yn/yd) { |
|||
count = count + 1 |
|||
omitted[k] = omitted[k] + 1 |
|||
if (count <= 12) { |
|||
Fmt.print("$d/$d => $d/$d (removed $d)", xn, xd, yn, yd, k+1) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Fmt.print("$d-digit fractions found:$d, omitted $s\n", n, count, omitted) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
16/64 => 1/4 (removed 6) |
|||
19/95 => 1/5 (removed 9) |
|||
26/65 => 2/5 (removed 6) |
|||
49/98 => 4/8 (removed 9) |
|||
2-digit fractions found:4, omitted 0 0 0 0 0 2 0 0 2 |
|||
132/231 => 12/21 (removed 3) |
|||
134/536 => 14/56 (removed 3) |
|||
134/938 => 14/98 (removed 3) |
|||
136/238 => 16/28 (removed 3) |
|||
138/345 => 18/45 (removed 3) |
|||
139/695 => 13/65 (removed 9) |
|||
143/341 => 13/31 (removed 4) |
|||
146/365 => 14/35 (removed 6) |
|||
149/298 => 14/28 (removed 9) |
|||
149/596 => 14/56 (removed 9) |
|||
149/894 => 14/84 (removed 9) |
|||
154/253 => 14/23 (removed 5) |
|||
3-digit fractions found:122, omitted 0 0 9 1 6 15 16 15 60 |
|||
1234/4936 => 124/496 (removed 3) |
|||
1239/6195 => 123/615 (removed 9) |
|||
1246/3649 => 126/369 (removed 4) |
|||
1249/2498 => 124/248 (removed 9) |
|||
1259/6295 => 125/625 (removed 9) |
|||
1279/6395 => 127/635 (removed 9) |
|||
1283/5132 => 128/512 (removed 3) |
|||
1297/2594 => 127/254 (removed 9) |
|||
1297/3891 => 127/381 (removed 9) |
|||
1298/2596 => 128/256 (removed 9) |
|||
1298/3894 => 128/384 (removed 9) |
|||
1298/5192 => 128/512 (removed 9) |
|||
4-digit fractions found:660, omitted 14 25 92 14 29 63 16 17 390 |
|||
12349/24698 => 1234/2468 (removed 9) |
|||
12356/67958 => 1236/6798 (removed 5) |
|||
12358/14362 => 1258/1462 (removed 3) |
|||
12358/15364 => 1258/1564 (removed 3) |
|||
12358/17368 => 1258/1768 (removed 3) |
|||
12358/19372 => 1258/1972 (removed 3) |
|||
12358/21376 => 1258/2176 (removed 3) |
|||
12358/25384 => 1258/2584 (removed 3) |
|||
12359/61795 => 1235/6175 (removed 9) |
|||
12364/32596 => 1364/3596 (removed 2) |
|||
12379/61895 => 1237/6185 (removed 9) |
|||
12386/32654 => 1386/3654 (removed 2) |
|||
5-digit fractions found:5087, omitted 75 40 376 78 209 379 591 351 2988 |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |