Fraction reduction: Difference between revisions
m (→{{header|Pascal}}: reduced to 12 solutions shown) |
m (→{{header|Pascal}}: 8-digits with fewer solutions?? Check for Uint64) |
||
Line 931: | Line 931: | ||
230 256 921 186 317 751 262 205 6650 |
230 256 921 186 317 751 262 205 6650 |
||
time taken 00:00:30.399 |
time taken 00:00:30.399 |
||
Used digits 7 |
|||
3 498276/124569 = 4938276/1234569 |
|||
3 195286/124579 = 1935286/1234579 |
|||
3 245791/124579 = 2435791/1234579 |
|||
3 286195/124579 = 2836195/1234579 |
|||
3 457912/124579 = 4537912/1234579 |
|||
3 528619/124579 = 5238619/1234579 |
|||
3 579124/124579 = 5739124/1234579 |
|||
3 619528/124579 = 6139528/1234579 |
|||
9 617285/123457 = 6172895/1234579 |
|||
9 617285/123457 = 6172985/1234597 |
|||
9 617325/123465 = 6173295/1234659 |
|||
3 498712/124678 = 4938712/1234678 |
|||
Found solutions 40163 |
|||
omitted digits 1 to 9 |
|||
333 191 1368 278 498 1094 3657 1434 31310 |
|||
time taken 00:04:42.891 |
|||
Used digits 8 |
|||
3 2457691/1245679 = 24357691/12345679 //2 solutions |
|||
6 2435791/1234579 = 24357691/12345679 // |
|||
3 4982716/1245679 = 49382716/12345679 |
|||
3 6194728/1245679 = 61394728/12345679 |
|||
9 6172835/1234567 = 61728395/12345679 |
|||
3 4982756/1245689 = 49382756/12345689 |
|||
9 6172835/1234567 = 61729835/12345967 |
|||
9 6173285/1234657 = 61732895/12346579 |
|||
9 6173285/1234657 = 61732985/12346597 |
|||
3 4987156/1246789 = 49387156/12346789 |
|||
9 6173425/1234685 = 61734295/12346859 |
|||
3 4987516/1246879 = 49387516/12346879 |
|||
Found solutions 17233 |
|||
omitted digits 1 to 9 |
|||
247 233 888 288 355 710 425 193 13894 |
|||
time taken 00:17:54.294 |
|||
/* |
/* |
||
go version go1.10.3 gccgo (Debian 8.3.0-6) 8.3.0 linux/amd64 |
go version go1.10.3 gccgo (Debian 8.3.0-6) 8.3.0 linux/amd64 |
Revision as of 13:04, 11 September 2019
There is a fine line between numerator and denominator. ─── anonymous
A method to "reduce" some reducible fractions is to cross out a digit from the numerator and the denominator. An example is:
16 16──── and then (simply) cross─out the sixes: ──── 6464
resulting in:
1 ─── 4
Naturally, this "method" of reduction must reduce to the proper value (shown as a fraction).
This "method" is also known as anomalous cancellation and also accidental cancellation.
(Of course, this "method" shouldn't be taught to impressionable or gullible minds.) 😇
- Task
Find and show some fractions that can be reduced by the above "method".
- show 2-digit fractions found (like the example shown above)
- show 3-digit fractions
- show 4-digit fractions
- show 5-digit fractions (and higher) (optional)
- show each (above) n-digit fractions separately from other different n-sized fractions, don't mix different "sizes" together
- for each "size" fraction, only show a dozen examples (the 1st twelve found)
- (it's recognized that not every programming solution will have the same generation algorithm)
- for each "size" fraction:
- show a count of how many reducible fractions were found. The example (above) is size 2
- show a count of which digits were crossed out (one line for each different digit)
- for each "size" fraction, show a count of how many were found. The example (above) is size 2
- show each n-digit example (to be shown on one line):
- show each n-digit fraction
- show each reduced n-digit fraction
- show what digit was crossed out for the numerator and the denominator
- Task requirements/restrictions
-
- only proper fractions and their reductions (the result) are to be used (no vulgar fractions)
- only positive fractions are to be used (no negative signs anywhere)
- only base ten integers are to be used for the numerator and denominator
- no zeros (decimal digit) can be used within the numerator or the denominator
- the numerator and denominator should be composed of the same number of digits
- no digit can be repeated in the numerator
- no digit can be repeated in the denominator
- (naturally) there should be a shared decimal digit in the numerator and the denominator
- fractions can be shown as 16/64 (for example)
Show all output here, on this page.
- Somewhat related task
-
- Farey sequence (It concerns fractions.)
- References
-
- Wikipedia entry: proper and improper fractions.
- Wikipedia entry: anomalous cancellation and/or accidental cancellation.
Go
Version 1
This produces the stats for 5-digit fractions in less than 25 seconds but takes a much longer 15.5 minutes to process the 6-digit case. Timings are for an Intel Core i7-8565U machine. <lang go>package main
import (
"fmt" "time"
)
func indexOf(n int, s []int) int {
for i, j := range s { if n == j { return i } } return -1
}
func getDigits(n, le int, digits []int) bool {
for n > 0 { r := n % 10 if r == 0 || indexOf(r, digits) >= 0 { return false } le-- digits[le] = r n /= 10 } return true
}
var pows = [5]int{1, 10, 100, 1000, 10000}
func removeDigit(digits []int, le, idx int) int {
sum := 0 pow := pows[le-2] for i := 0; i < le; i++ { if i == idx { continue } sum += digits[i] * pow pow /= 10 } return sum
}
func main() {
start := time.Now() lims := [5][2]int{ {12, 97}, {123, 986}, {1234, 9875}, {12345, 98764}, {123456, 987653}, } var count [5]int var omitted [5][10]int for i, lim := range lims { nDigits := make([]int, i+2) dDigits := make([]int, i+2) blank := make([]int, i+2) for n := lim[0]; n <= lim[1]; n++ { copy(nDigits, blank) nOk := getDigits(n, i+2, nDigits) if !nOk { continue } for d := n + 1; d <= lim[1]+1; d++ { copy(dDigits, blank) dOk := getDigits(d, i+2, dDigits) if !dOk { continue } for nix, digit := range nDigits { if dix := indexOf(digit, dDigits); dix >= 0 { rn := removeDigit(nDigits, i+2, nix) rd := removeDigit(dDigits, i+2, dix) if float64(n)/float64(d) == float64(rn)/float64(rd) { count[i]++ omitted[i][digit]++ if count[i] <= 12 { fmt.Printf("%d/%d = %d/%d by omitting %d's\n", n, d, rn, rd, digit) } } } } } } fmt.Println() }
for i := 2; i <= 6; i++ { fmt.Printf("There are %d %d-digit fractions of which:\n", count[i-2], i) for j := 1; j <= 9; j++ { if omitted[i-2][j] == 0 { continue } fmt.Printf("%6d have %d's omitted\n", omitted[i-2][j], j) } fmt.Println() } fmt.Printf("Took %s\n", time.Since(start))
}</lang>
- Output:
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 123459/617295 = 12345/61725 by omitting 9's 123468/493872 = 12468/49872 by omitting 3's 123469/173524 = 12469/17524 by omitting 3's 123469/193546 = 12469/19546 by omitting 3's 123469/213568 = 12469/21568 by omitting 3's 123469/283645 = 12469/28645 by omitting 3's 123469/493876 = 12469/49876 by omitting 3's 123469/573964 = 12469/57964 by omitting 3's 123479/617395 = 12347/61735 by omitting 9's 123495/172893 = 12345/17283 by omitting 9's 123548/679514 = 12348/67914 by omitting 5's 123574/325786 = 13574/35786 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 There are 9778 6-digit fractions of which: 230 have 1's omitted 256 have 2's omitted 921 have 3's omitted 186 have 4's omitted 317 have 5's omitted 751 have 6's omitted 262 have 7's omitted 205 have 8's omitted 6650 have 9's omitted Took 15m38.231915709s
Version 2
Rather than iterate through all numbers in the n-digit range and check if they contain unique non-zero digits, this generates all such numbers to start with which turns out to be a much more efficient approach - more than 20 times faster than before. <lang go>package main
import (
"fmt" "time"
)
type result struct {
n int nine [9]int
}
func indexOf(n int, s []int) int {
for i, j := range s { if n == j { return i } } return -1
}
func bIndexOf(b bool, s []bool) int {
for i, j := range s { if b == j { return i } } return -1
}
func toNumber(digits []int, removeDigit int) int {
digits2 := digits if removeDigit != 0 { digits2 = make([]int, len(digits)) copy(digits2, digits) d := indexOf(removeDigit, digits2) copy(digits2[d:], digits2[d+1:]) digits2[len(digits2)-1] = 0 digits2 = digits2[:len(digits2)-1] } res := digits2[0] for i := 1; i < len(digits2); i++ { res = res*10 + digits2[i] } return res
}
func nDigits(n int) []result {
var res []result digits := make([]int, n) var used [9]bool for i := 0; i < n; i++ { digits[i] = i + 1 used[i] = true } for { var nine [9]int for i := 0; i < len(used); i++ { if used[i] { nine[i] = toNumber(digits, i+1) } } res = append(res, result{toNumber(digits, 0), nine}) found := false for i := n - 1; i >= 0; i-- { d := digits[i] if !used[d-1] { panic("something went wrong with 'used' array") } used[d-1] = false for j := d; j < 9; j++ { if !used[j] { used[j] = true digits[i] = j + 1 for k := i + 1; k < n; k++ { digits[k] = bIndexOf(false, used[:]) + 1 used[digits[k]-1] = true } found = true break } } if found { break } } if !found { break } } return res
}
func main() {
start := time.Now() for n := 2; n <= 5; n++ { rs := nDigits(n) count := 0 var omitted [9]int for i := 0; i < len(rs)-1; i++ { xn, rn := rs[i].n, rs[i].nine for j := i + 1; j < len(rs); j++ { xd, rd := rs[j].n, rs[j].nine for k := 0; k < 9; k++ { yn, yd := rn[k], rd[k] if yn != 0 && yd != 0 && float64(xn)/float64(xd) == float64(yn)/float64(yd) { count++ omitted[k]++ if count <= 12 { fmt.Printf("%d/%d => %d/%d (removed %d)\n", xn, xd, yn, yd, k+1) } } } } } fmt.Printf("%d-digit fractions found:%d, omitted %v\n\n", n, count, omitted) } fmt.Printf("Took %s\n", time.Since(start))
}</lang>
- Output:
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] 123459/617295 => 12345/61725 (removed 9) 123468/493872 => 12468/49872 (removed 3) 123469/173524 => 12469/17524 (removed 3) 123469/193546 => 12469/19546 (removed 3) 123469/213568 => 12469/21568 (removed 3) 123469/283645 => 12469/28645 (removed 3) 123469/493876 => 12469/49876 (removed 3) 123469/573964 => 12469/57964 (removed 3) 123479/617395 => 12347/61735 (removed 9) 123495/172893 => 12345/17283 (removed 9) 123548/679514 => 12348/67914 (removed 5) 123574/325786 => 13574/35786 (removed 2) 6-digit fractions found:9778, omitted [230 256 921 186 317 751 262 205 6650] Took 42.251172302s
Julia
<lang julia>using Combinatorics
toi(set) = parse(Int, join(set, "")) drop1(c, set) = toi(filter(x -> x != c, set))
function anomalouscancellingfractions(numdigits)
ret = Vector{Tuple{Int, Int, Int, Int, Int}}() for nset in permutations(1:9, numdigits), dset in permutations(1:9, numdigits) if nset < dset # only proper fractions for c in nset if c in dset # a common digit exists n, d, nn, dd = toi(nset), toi(dset), drop1(c, nset), drop1(c, dset) if n // d == nn // dd # anomalous cancellation push!(ret, (n, d, nn, dd, c)) end end end end end ret
end
function testfractionreduction(maxdigits=5)
for i in 2:maxdigits results = anomalouscancellingfractions(i) println("\nFor $i digits, there were ", length(results), " fractions with anomalous cancellation.") numcounts = zeros(Int, 9) for r in results numcounts[r[5]] += 1 end for (j, count) in enumerate(numcounts) count > 0 && println("The digit $j was crossed out $count times.") end println("Examples:") for j in 1:min(length(results), 12) r = results[j] println(r[1], "/", r[2], " = ", r[3], "/", r[4], " ($(r[5]) crossed out)") end end
end
testfractionreduction()
</lang>
- Output:
For 2 digits, there were 4 fractions with anomalous cancellation. The digit 6 was crossed out 2 times. The digit 9 was crossed out 2 times. Examples: 16/64 = 1/4 (6 crossed out) 19/95 = 1/5 (9 crossed out) 26/65 = 2/5 (6 crossed out) 49/98 = 4/8 (9 crossed out) For 3 digits, there were 122 fractions with anomalous cancellation. The digit 3 was crossed out 9 times. The digit 4 was crossed out 1 times. The digit 5 was crossed out 6 times. The digit 6 was crossed out 15 times. The digit 7 was crossed out 16 times. The digit 8 was crossed out 15 times. The digit 9 was crossed out 60 times. Examples: 132/231 = 12/21 (3 crossed out) 134/536 = 14/56 (3 crossed out) 134/938 = 14/98 (3 crossed out) 136/238 = 16/28 (3 crossed out) 138/345 = 18/45 (3 crossed out) 139/695 = 13/65 (9 crossed out) 143/341 = 13/31 (4 crossed out) 146/365 = 14/35 (6 crossed out) 149/298 = 14/28 (9 crossed out) 149/596 = 14/56 (9 crossed out) 149/894 = 14/84 (9 crossed out) 154/253 = 14/23 (5 crossed out) For 4 digits, there were 660 fractions with anomalous cancellation. The digit 1 was crossed out 14 times. The digit 2 was crossed out 25 times. The digit 3 was crossed out 92 times. The digit 4 was crossed out 14 times. The digit 5 was crossed out 29 times. The digit 6 was crossed out 63 times. The digit 7 was crossed out 16 times. The digit 8 was crossed out 17 times. The digit 9 was crossed out 390 times. Examples: 1234/4936 = 124/496 (3 crossed out) 1239/6195 = 123/615 (9 crossed out) 1246/3649 = 126/369 (4 crossed out) 1249/2498 = 124/248 (9 crossed out) 1259/6295 = 125/625 (9 crossed out) 1279/6395 = 127/635 (9 crossed out) 1283/5132 = 128/512 (3 crossed out) 1297/2594 = 127/254 (9 crossed out) 1297/3891 = 127/381 (9 crossed out) 1298/2596 = 128/256 (9 crossed out) 1298/3894 = 128/384 (9 crossed out) 1298/5192 = 128/512 (9 crossed out) For 5 digits, there were 5087 fractions with anomalous cancellation. The digit 1 was crossed out 75 times. The digit 2 was crossed out 40 times. The digit 3 was crossed out 376 times. The digit 4 was crossed out 78 times. The digit 5 was crossed out 209 times. The digit 6 was crossed out 379 times. The digit 7 was crossed out 591 times. The digit 8 was crossed out 351 times. The digit 9 was crossed out 2988 times. Examples: 12349/24698 = 1234/2468 (9 crossed out) 12356/67958 = 1236/6798 (5 crossed out) 12358/14362 = 1258/1462 (3 crossed out) 12358/15364 = 1258/1564 (3 crossed out) 12358/17368 = 1258/1768 (3 crossed out) 12358/19372 = 1258/1972 (3 crossed out) 12358/21376 = 1258/2176 (3 crossed out) 12358/25384 = 1258/2584 (3 crossed out) 12359/61795 = 1235/6175 (9 crossed out) 12364/32596 = 1364/3596 (2 crossed out) 12379/61895 = 1237/6185 (9 crossed out) 12386/32654 = 1386/3654 (2 crossed out)
Pascal
Using a permutation k out of n with k <= n
Inserting this number and all numbers with one digit removed of that number.So only once calculated.Trade off is big size and no cache friendly local access.
<lang pascal>
program FracRedu;
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$CODEALIGN proc=16,loop=4}
{$ELSE}
{$APPTYPE CONSOLE}
{$ENDIF} uses
SysUtils;
type
tdigit = 0..9;
const
cMaskDgt: array [tdigit] of Uint32 = (1, 2, 4, 8, 16, 32, 64, 128, 256, 512 {,1024,2048,4096,8193,16384,32768}); cMaxDigits = High(tdigit);
type
tPermfield = array[tdigit] of uint32; tpPermfield = ^tPermfield; tDigitCnt = array[tdigit] of Uint32;
tErg = record numUsedDigits : Uint32;//Bitfield of used digits dummy : array[0..7-4] of Byte;//Align to 8 numUnusedDigit : array[tdigit] of Uint32;//Index 0 == number with all digits end; tpErg = ^tErg;
var
Erg: array of tErg; pf_x, pf_y: tPermfield; DigitCnt :tDigitCnt; permcnt, UsedDigits,Anzahl: NativeUint;
function Fakultaet(i: integer): integer; begin Result := 1; while i > 1 do begin Result := Result * i; Dec(i); end; end;
function BinomCoeff(n, k: byte): longint; var i: longint; begin {n ueber k = n ueber (n-k) , using shorter version} if k > n div 2 then k := n - k; Result := 1; if k <= n then for i := 1 to k do Result := Result * (n - i + 1) div i;{geht immer ohne Rest } end;
procedure OutErg(dgt,nom1,dom1,nom2,dom2: Uint32); begin writeln(dgt:3,' ',nom1,'/',dom1,' = ',nom2,'/',dom2); end;
function Check(pI,pJ : tpErg;Nud :Word):integer; var dgt,nj,ni: NativeInt; Begin ni := pI^.numUnusedDigit[0]; nj := pJ^.numUnusedDigit[0]; result := 0; dgt := 1; NUD := NUD SHR 1; repeat IF NUD AND 1 <> 0 then Begin If ni*pJ^.numUnusedDigit[dgt] = nj*pI^.numUnusedDigit[dgt] then Begin inc(result); inc(DigitCnt[dgt]); IF Anzahl < 12 then OutErg(dgt,pJ^.numUnusedDigit[dgt],pI^.numUnusedDigit[dgt],nj,ni); end; end; inc(dgt); NUD := NUD SHR 1; until NUD = 0; end;
procedure CheckWithOne(pI : tpErg;j,Nud:Uint32); var pJ : tpErg; l : NativeUInt; Begin pJ := pI; // UsedDigits >= 5 then always one digit is in both permutations if UsedDigits <5 then Begin for j := j+1 to permcnt do begin inc(pJ); //digits used by both numbers l := NUD AND pJ^.numUsedDigits; IF l <> 0 then inc(Anzahl,Check(pI,pJ,l)); end; end else Begin for j := j+1 to permcnt do begin inc(pJ); l := NUD AND pJ^.numUsedDigits; inc(Anzahl,Check(pI,pJ,l)); end; end; end;
procedure SearchMultiple; var pI : tpErg; i : NativeUInt; begin pI := @Erg[0]; for i := 0 to permcnt do Begin CheckWithOne(pI,i,pI^.numUsedDigits); inc(pI); end; end;
procedure InsertToErg(var E: tErg; const x: tPermfield; k: NativeInt); var i,j,n, dgt,nud: NativeInt; begin n := 0; nud := 0; for i := 1 to k do begin dgt := x[i]; nud := nud or cMaskDgt[dgt]; n := n * 10 + dgt; end; with E do begin numUsedDigits := nud; numUnusedDigit[0] := n; end; //calc all numbers with one removed digit For J := k downto 1 do Begin n := 0; for i := 1 to j-1 do n := n * 10 + x[i]; for i := j+1 to k do n := n * 10 + x[i]; E.numUnusedDigit[x[j]] := n; end; end;
procedure PermKoutofN(k, n: nativeInt); //http://rosettacode.org/mw/index.php?title=Permutations§ion=93#alternative var x, y: tpPermfield; i, yi, tmp: NativeInt; begin //initialise x := @pf_x; y := @pf_y; permcnt := 0; if k > n then k := n; if k = n then k := k - 1; for i := 1 to n do x^[i] := i; for i := 1 to k do y^[i] := i;
InserttoErg(Erg[permcnt], x^, k); i := k; repeat yi := y^[i]; if yi < n then begin Inc(permcnt); Inc(yi); y^[i] := yi; tmp := x^[i]; x^[i] := x^[yi]; x^[yi] := tmp; i := k; InserttoErg(Erg[permcnt], x^, k); end else begin repeat tmp := x^[i]; x^[i] := x^[yi]; x^[yi] := tmp; Dec(yi); until yi <= i; y^[i] := yi; Dec(i); end; until (i = 0); end; procedure OutDigitCount; var i : tDigit; Begin writeln('omitted digits 1 to 9'); For i := 1 to 9do write(DigitCnt[i]:UsedDigits); writeln; end;
procedure ClearDigitCount; var i : tDigit; Begin For i := low(DigitCnt) to high(DigitCnt) do DigitCnt[i] := 0; end;
var
t1, t0: TDateTime;
begin
For UsedDigits := 2 to 6 do Begin writeln('Used digits ',UsedDigits); T0 := now; ClearDigitCount; setlength(Erg, Fakultaet(UsedDigits) * BinomCoeff(cMaxDigits, UsedDigits)); Anzahl := 0; permcnt := 0; PermKoutOfN(UsedDigits, cMaxDigits); SearchMultiple; T1 := now; writeln('Found solutions ',Anzahl); OutDigitCount; writeln('time taken ',FormatDateTime('HH:NN:SS.zzz', T1 - T0)); setlength(Erg, 0); writeln; end;
end.</lang>
- Output:
Used digits 2 6 4/1 = 64/16 9 5/1 = 95/19 6 5/2 = 65/26 9 8/4 = 98/49 Found solutions 4 omitted digits 1 to 9 0 0 0 0 0 2 0 0 2 time taken 00:00:00.000 Used digits 3 3 21/12 = 231/132 3 56/14 = 536/134 3 98/14 = 938/134 3 28/16 = 238/136 3 45/18 = 345/138 9 65/13 = 695/139 4 31/13 = 341/143 6 35/14 = 365/146 9 28/14 = 298/149 9 56/14 = 596/149 9 84/14 = 894/149 5 23/14 = 253/154 Found solutions 122 omitted digits 1 to 9 0 0 9 1 6 15 16 15 60 time taken 00:00:00.004 Used digits 4 3 496/124 = 4936/1234 9 615/123 = 6195/1239 4 369/126 = 3649/1246 9 248/124 = 2498/1249 9 625/125 = 6295/1259 9 635/127 = 6395/1279 3 512/128 = 5132/1283 9 254/127 = 2594/1297 9 381/127 = 3891/1297 9 256/128 = 2596/1298 9 384/128 = 3894/1298 9 512/128 = 5192/1298 Found solutions 660 omitted digits 1 to 9 14 25 92 14 29 63 16 17 390 time taken 00:00:00.056 Used digits 5 9 2468/1234 = 24698/12349 5 6798/1236 = 67958/12356 3 1462/1258 = 14362/12358 3 1564/1258 = 15364/12358 3 1768/1258 = 17368/12358 3 1972/1258 = 19372/12358 3 2176/1258 = 21376/12358 3 2584/1258 = 25384/12358 9 6175/1235 = 61795/12359 2 3596/1364 = 32596/12364 9 6185/1237 = 61895/12379 2 3654/1386 = 32654/12386 Found solutions 5087 omitted digits 1 to 9 75 40 376 78 209 379 591 351 2988 time taken 00:00:01.692 Used digits 6 9 61725/12345 = 617295/123459 3 49872/12468 = 493872/123468 3 17524/12469 = 173524/123469 3 19546/12469 = 193546/123469 3 21568/12469 = 213568/123469 3 28645/12469 = 283645/123469 3 49876/12469 = 493876/123469 3 57964/12469 = 573964/123469 9 61735/12347 = 617395/123479 9 17283/12345 = 172893/123495 5 67914/12348 = 679514/123548 2 35786/13574 = 325786/123574 Found solutions 9778 omitted digits 1 to 9 230 256 921 186 317 751 262 205 6650 time taken 00:00:30.399 Used digits 7 3 498276/124569 = 4938276/1234569 3 195286/124579 = 1935286/1234579 3 245791/124579 = 2435791/1234579 3 286195/124579 = 2836195/1234579 3 457912/124579 = 4537912/1234579 3 528619/124579 = 5238619/1234579 3 579124/124579 = 5739124/1234579 3 619528/124579 = 6139528/1234579 9 617285/123457 = 6172895/1234579 9 617285/123457 = 6172985/1234597 9 617325/123465 = 6173295/1234659 3 498712/124678 = 4938712/1234678 Found solutions 40163 omitted digits 1 to 9 333 191 1368 278 498 1094 3657 1434 31310 time taken 00:04:42.891 Used digits 8 3 2457691/1245679 = 24357691/12345679 //2 solutions 6 2435791/1234579 = 24357691/12345679 // 3 4982716/1245679 = 49382716/12345679 3 6194728/1245679 = 61394728/12345679 9 6172835/1234567 = 61728395/12345679 3 4982756/1245689 = 49382756/12345689 9 6172835/1234567 = 61729835/12345967 9 6173285/1234657 = 61732895/12346579 9 6173285/1234657 = 61732985/12346597 3 4987156/1246789 = 49387156/12346789 9 6173425/1234685 = 61734295/12346859 3 4987516/1246879 = 49387516/12346879 Found solutions 17233 omitted digits 1 to 9 247 233 888 288 355 710 425 193 13894 time taken 00:17:54.294 /* go version go1.10.3 gccgo (Debian 8.3.0-6) 8.3.0 linux/amd64 6-digit fractions found:9778, omitted [230 256 921 186 317 751 262 205 6650] Took 1m38.85577279s */
MiniZinc
The Model
<lang MiniZinc> %Latin Squares in Reduced Form. 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
- Output:
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 ---------- ==========
- minizinc --num-solutions 12 -DS=3
- Output:
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 ----------
- minizinc --num-solutions 12 -DS=4
- Output:
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 ----------
- minizinc --num-solutions 12 -DS=5
- Output:
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 ----------
- minizinc --num-solutions 12 -DS=6
- Output:
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 ----------
- Count number of solutions
- minizinc --all-solutions -s -DS=3
- Output:
%%%mzn-stat: nSolutions=122
- minizinc --all-solutions -s -DS=4
- Output:
%%%mzn-stat: nSolutions=660
- minizinc --all-solutions -s -DS=5
- Output:
%%%mzn-stat: nSolutions=5087
Perl
<lang perl>use strict; use warnings; use feature 'say'; use List::Util qw<sum uniq uniqnum head tail>;
for my $exp (map { $_ - 1 } <2 3 4>) {
my %reduced; my $start = sum map { 10 ** $_ * ($exp - $_ + 1) } 0..$exp; my $end = 10**($exp+1) - -1 + sum map { 10 ** $_ * ($exp - $_) } 0..$exp-1;
for my $den ($start .. $end-1) { next if $den =~ /0/ or (uniqnum split , $den) <= $exp; for my $num ($start .. $den-1) { next if $num =~ /0/ or (uniqnum split , $num) <= $exp; my %i; map { $i{$_}++ } (uniq head -1, split ,$den), uniq tail -1, split ,$num; my @set = grep { $_ if $i{$_} > 1 } keys %i; next if @set < 1; for (@set) { (my $ne = $num) =~ s/$_//; (my $de = $den) =~ s/$_//; if ($ne/$de == $num/$den) { $reduced{"$num/$den:$_"} = "$ne/$de"; } } } } my $digit = $exp + 1; say "\n" . +%reduced . " $digit-digit reducible fractions:"; for my $n (1..9) { my $cnt = scalar grep { /:$n/ } keys %reduced; say "$cnt with removed $n" if $cnt; } say "\n 12 (or all, if less) $digit-digit reducible fractions:"; for my $f (head 12, sort keys %reduced) { printf " %s => %s removed %s\n", substr($f,0,$digit*2+1), $reduced{$f}, substr($f,-1) }
}</lang>
- Output:
4 2-digit reducible fractions: 2 with removed 6 2 with removed 9 12 (or all, if less) 2-digit reducible 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 reducible 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 (or all, if less) 3-digit reducible fractions: 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 660 4-digit reducible 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 (or all, if less) 4-digit reducible fractions: 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
Perl 6
<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>
- Sample output:
(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
Phix
<lang Phix>function to_n(sequence digits, integer remove_digit=0)
if remove_digit!=0 then integer d = find(remove_digit,digits) digits[d..d] = {} end if integer res = digits[1] for i=2 to length(digits) do res = res*10+digits[i] end for return res
end function
function ndigits(integer n) -- generate numbers with unique digits efficiently -- and store them in an array for multiple re-use, -- along with an array of the removed-digit values.
sequence res = {}, digits = tagset(n), used = repeat(1,n)&repeat(0,9-n) while true do sequence nine = repeat(0,9) for i=1 to length(used) do if used[i] then nine[i] = to_n(digits,i) end if end for res = append(res,{to_n(digits),nine}) bool found = false for i=n to 1 by -1 do integer d = digits[i] if not used[d] then ?9/0 end if used[d] = 0 for j=d+1 to 9 do if not used[j] then used[j] = 1 digits[i] = j for k=i+1 to n do digits[k] = find(0,used) used[digits[k]] = 1 end for found = true exit end if end for if found then exit end if end for if not found then exit end if end while return res
end function
atom t0 = time(),
t1 = time()+1
for n=2 to 6 do
sequence d = ndigits(n) integer count = 0 sequence omitted = repeat(0,9) for i=1 to length(d)-1 do {integer xn, sequence rn} = d[i] for j=i+1 to length(d) do {integer xd, sequence rd} = d[j] for k=1 to 9 do integer yn = rn[k], yd = rd[k] if yn!=0 and yd!=0 and xn/xd = yn/yd then count += 1 omitted[k] += 1 if count<=12 then printf(1,"%d/%d => %d/%d (removed %d)\n",{xn,xd,yn,yd,k}) elsif time()>t1 then printf(1,"working (%d/%d)...\r",{i,length(d)}) t1 = time()+1 end if end if end for end for end for printf(1,"%d-digit fractions found:%d, omitted %v\n\n",{n,count,omitted})
end for ?elapsed(time()-t0)</lang>
- Output:
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} 123459/617295 => 12345/61725 (removed 9) 123468/493872 => 12468/49872 (removed 3) 123469/173524 => 12469/17524 (removed 3) 123469/193546 => 12469/19546 (removed 3) 123469/213568 => 12469/21568 (removed 3) 123469/283645 => 12469/28645 (removed 3) 123469/493876 => 12469/49876 (removed 3) 123469/573964 => 12469/57964 (removed 3) 123479/617395 => 12347/61735 (removed 9) 123495/172893 => 12345/17283 (removed 9) 123548/679514 => 12348/67914 (removed 5) 123574/325786 => 13574/35786 (removed 2) 6-digit fractions found:9778, omitted {230,256,921,186,317,751,262,205,6650} "10 minutes and 13s"
Racket
Racket's generator is horribly slow, so I roll my own more efficient generator. Pretty much using continuation-passing style, but then using macro to make it appear that we are writing in the direct style.
<lang racket>#lang racket
(require racket/generator
syntax/parse/define)
(define-syntax-parser for**
[(_ [x:id {~datum <-} (e ...)] rst ...) #'(e ... (λ (x) (for** rst ...)))] [(_ e ...) #'(begin e ...)])
(define (permutations xs n yield #:lower [lower #f])
(let loop ([xs xs] [n n] [acc '()] [lower lower]) (cond [(= n 0) (yield (reverse acc))] [else (for ([x (in-list xs)] #:when (or (not lower) (>= x (first lower)))) (loop (remove x xs) (sub1 n) (cons x acc) (and lower (= x (first lower)) (rest lower))))])))
(define (list->number xs) (foldl (λ (e acc) (+ (* 10 acc) e)) 0 xs))
(define (calc n)
(define rng (range 1 10)) (in-generator (for** [numer <- (permutations rng n)] [denom <- (permutations rng n #:lower numer)] (for* (#:when (not (equal? numer denom)) [crossed (in-list numer)] #:when (member crossed denom) [numer* (in-value (list->number (remove crossed numer)))] [denom* (in-value (list->number (remove crossed denom)))] [numer** (in-value (list->number numer))] [denom** (in-value (list->number denom))] #:when (= (* numer** denom*) (* numer* denom**))) (yield (list numer** denom** numer* denom* crossed))))))
(define (enumerate n)
(for ([x (calc n)] [i (in-range 12)]) (apply printf "~a/~a = ~a/~a (~a crossed out)\n" x)) (newline))
(define (stats n)
(define digits (make-hash)) (for ([x (calc n)]) (hash-update! digits (last x) add1 0)) (printf "There are ~a ~a-digit fractions of which:\n" (for/sum ([(k v) (in-hash digits)]) v) n) (for ([digit (in-list (sort (hash->list digits) < #:key car))]) (printf " The digit ~a was crossed out ~a times\n" (car digit) (cdr digit))) (newline))
(define (main)
(enumerate 2) (enumerate 3) (enumerate 4) (enumerate 5) (stats 2) (stats 3) (stats 4) (stats 5))
(main)</lang>
- Output:
16/64 = 1/4 (6 crossed out) 19/95 = 1/5 (9 crossed out) 26/65 = 2/5 (6 crossed out) 49/98 = 4/8 (9 crossed out) 132/231 = 12/21 (3 crossed out) 134/536 = 14/56 (3 crossed out) 134/938 = 14/98 (3 crossed out) 136/238 = 16/28 (3 crossed out) 138/345 = 18/45 (3 crossed out) 139/695 = 13/65 (9 crossed out) 143/341 = 13/31 (4 crossed out) 146/365 = 14/35 (6 crossed out) 149/298 = 14/28 (9 crossed out) 149/596 = 14/56 (9 crossed out) 149/894 = 14/84 (9 crossed out) 154/253 = 14/23 (5 crossed out) 1234/4936 = 124/496 (3 crossed out) 1239/6195 = 123/615 (9 crossed out) 1246/3649 = 126/369 (4 crossed out) 1249/2498 = 124/248 (9 crossed out) 1259/6295 = 125/625 (9 crossed out) 1279/6395 = 127/635 (9 crossed out) 1283/5132 = 128/512 (3 crossed out) 1297/2594 = 127/254 (9 crossed out) 1297/3891 = 127/381 (9 crossed out) 1298/2596 = 128/256 (9 crossed out) 1298/3894 = 128/384 (9 crossed out) 1298/5192 = 128/512 (9 crossed out) 12349/24698 = 1234/2468 (9 crossed out) 12356/67958 = 1236/6798 (5 crossed out) 12358/14362 = 1258/1462 (3 crossed out) 12358/15364 = 1258/1564 (3 crossed out) 12358/17368 = 1258/1768 (3 crossed out) 12358/19372 = 1258/1972 (3 crossed out) 12358/21376 = 1258/2176 (3 crossed out) 12358/25384 = 1258/2584 (3 crossed out) 12359/61795 = 1235/6175 (9 crossed out) 12364/32596 = 1364/3596 (2 crossed out) 12379/61895 = 1237/6185 (9 crossed out) 12386/32654 = 1386/3654 (2 crossed out) There are 4 2-digit fractions of which: The digit 6 was crossed out 2 times The digit 9 was crossed out 2 times There are 122 3-digit fractions of which: The digit 3 was crossed out 9 times The digit 4 was crossed out 1 times The digit 5 was crossed out 6 times The digit 6 was crossed out 15 times The digit 7 was crossed out 16 times The digit 8 was crossed out 15 times The digit 9 was crossed out 60 times There are 660 4-digit fractions of which: The digit 1 was crossed out 14 times The digit 2 was crossed out 25 times The digit 3 was crossed out 92 times The digit 4 was crossed out 14 times The digit 5 was crossed out 29 times The digit 6 was crossed out 63 times The digit 7 was crossed out 16 times The digit 8 was crossed out 17 times The digit 9 was crossed out 390 times There are 5087 5-digit fractions of which: The digit 1 was crossed out 75 times The digit 2 was crossed out 40 times The digit 3 was crossed out 376 times The digit 4 was crossed out 78 times The digit 5 was crossed out 209 times The digit 6 was crossed out 379 times The digit 7 was crossed out 591 times The digit 8 was crossed out 351 times The digit 9 was crossed out 2988 times
REXX
<lang rexx>/*REXX pgm reduces fractions by "crossing out" matching digits in nominator&denominator.*/ parse arg high show . /*obtain optional arguments from the CL*/ if high== | high=="," then high= 4 /*Not specified? Then use the default.*/ if show== | show=="," then show= 12 /* " " " " " " */ say center(' some samples of reduced fractions by crossing out digits ', 79, "═") $.=0 /*placeholder array for counts; init. 0*/
do L=2 to high; say /*do 2-dig fractions to HIGH-dig fract.*/ lim= 10**L - 1 /*calculate the upper limit just once. */ do n=10**(L-1) to lim /*generate some N digit fractions. */ if pos(0, n) \==0 then iterate /*Does it have a zero? Then skip it.*/ if hasDup(n) then iterate /* " " " " dup? " " " */
do d=n+1 to lim /*only process like-sized #'s */ if pos(0, d)\==0 then iterate /*Have a zero? Then skip it. */ if verify(d, n, 'M')==0 then iterate /*No digs in common? Skip it.*/ if hasDup(d) then iterate /*Any digs are dups? " " */ q= n/d /*compute quotient just once. */ do e=1 for L; xo= substr(n, e, 1) /*try crossing out each digit.*/ nn= space( translate(n, , xo), 0) /*elide from the numerator. */ dd= space( translate(d, , xo), 0) /* " " " denominator. */ if nn/dd \== q then iterate /*Not the same quotient? Skip.*/ $.L= $.L + 1 /*Eureka! We found one. */ $.L.xo= $.L.xo + 1 /*count the silly reduction. */ if $.L>show then iterate /*Too many found? Don't show.*/ say center(n'/'d " = " nn'/'dd " by crossing out the" xo"'s.", 79) end /*e*/ end /*d*/ end /*n*/ end /*L*/
say; @with= ' with crossed-out' /* [↓] show counts for any reductions.*/
do k=1 for 9 /*traipse through each cross─out digit.*/ if $.k==0 then iterate /*Is this a zero count? Then skip it. */ say; say center('There are ' $.k " "k'-digit fractions.', 79, "═") @for= ' For ' /*literal for SAY indentation (below). */ do #=1 for 9; if $.k.#==0 then iterate say @for k"-digit fractions, there are " right($.k.#, k-1) @with #"'s." end /*#*/ end /*k*/
exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ hasDup: parse arg x; /* if L<2 then return 0 */ /*L will never be 1.*/
do i=1 for L-1; if pos(substr(x,i,1), substr(x,i+1)) \== 0 then return 1 end /*i*/; return 0</lang>
- output when using the input of: 5 12
══════════ some samples of reduced fractions by crossing out digits ═══════════ 16/64 = 1/4 by crossing out the 6's. 19/95 = 1/5 by crossing out the 9's. 26/65 = 2/5 by crossing out the 6's. 49/98 = 4/8 by crossing out the 9's. 132/231 = 12/21 by crossing out the 3's. 134/536 = 14/56 by crossing out the 3's. 134/938 = 14/98 by crossing out the 3's. 136/238 = 16/28 by crossing out the 3's. 138/345 = 18/45 by crossing out the 3's. 139/695 = 13/65 by crossing out the 9's. 143/341 = 13/31 by crossing out the 4's. 146/365 = 14/35 by crossing out the 6's. 149/298 = 14/28 by crossing out the 9's. 149/596 = 14/56 by crossing out the 9's. 149/894 = 14/84 by crossing out the 9's. 154/253 = 14/23 by crossing out the 5's. 1234/4936 = 124/496 by crossing out the 3's. 1239/6195 = 123/615 by crossing out the 9's. 1246/3649 = 126/369 by crossing out the 4's. 1249/2498 = 124/248 by crossing out the 9's. 1259/6295 = 125/625 by crossing out the 9's. 1279/6395 = 127/635 by crossing out the 9's. 1283/5132 = 128/512 by crossing out the 3's. 1297/2594 = 127/254 by crossing out the 9's. 1297/3891 = 127/381 by crossing out the 9's. 1298/2596 = 128/256 by crossing out the 9's. 1298/3894 = 128/384 by crossing out the 9's. 1298/5192 = 128/512 by crossing out the 9's. 12349/24698 = 1234/2468 by crossing out the 9's. 12356/67958 = 1236/6798 by crossing out the 5's. 12358/14362 = 1258/1462 by crossing out the 3's. 12358/15364 = 1258/1564 by crossing out the 3's. 12358/17368 = 1258/1768 by crossing out the 3's. 12358/19372 = 1258/1972 by crossing out the 3's. 12358/21376 = 1258/2176 by crossing out the 3's. 12358/25384 = 1258/2584 by crossing out the 3's. 12359/61795 = 1235/6175 by crossing out the 9's. 12364/32596 = 1364/3596 by crossing out the 2's. 12379/61895 = 1237/6185 by crossing out the 9's. 12386/32654 = 1386/3654 by crossing out the 2's. ═══════════════════════There are 4 2-digit fractions.════════════════════════ For 2-digit fractions, there are 2 with crossed-out 6's. For 2-digit fractions, there are 2 with crossed-out 9's. ══════════════════════There are 122 3-digit fractions.═══════════════════════ For 3-digit fractions, there are 9 with crossed-out 3's. For 3-digit fractions, there are 1 with crossed-out 4's. For 3-digit fractions, there are 6 with crossed-out 5's. For 3-digit fractions, there are 15 with crossed-out 6's. For 3-digit fractions, there are 16 with crossed-out 7's. For 3-digit fractions, there are 15 with crossed-out 8's. For 3-digit fractions, there are 60 with crossed-out 9's. ══════════════════════There are 660 4-digit fractions.═══════════════════════ For 4-digit fractions, there are 14 with crossed-out 1's. For 4-digit fractions, there are 25 with crossed-out 2's. For 4-digit fractions, there are 92 with crossed-out 3's. For 4-digit fractions, there are 14 with crossed-out 4's. For 4-digit fractions, there are 29 with crossed-out 5's. For 4-digit fractions, there are 63 with crossed-out 6's. For 4-digit fractions, there are 16 with crossed-out 7's. For 4-digit fractions, there are 17 with crossed-out 8's. For 4-digit fractions, there are 390 with crossed-out 9's. ══════════════════════There are 5087 5-digit fractions.══════════════════════ For 5-digit fractions, there are 75 with crossed-out 1's. For 5-digit fractions, there are 40 with crossed-out 2's. For 5-digit fractions, there are 376 with crossed-out 3's. For 5-digit fractions, there are 78 with crossed-out 4's. For 5-digit fractions, there are 209 with crossed-out 5's. For 5-digit fractions, there are 379 with crossed-out 6's. For 5-digit fractions, there are 591 with crossed-out 7's. For 5-digit fractions, there are 351 with crossed-out 8's. For 5-digit fractions, there are 2988 with crossed-out 9's.