Fraction reduction: Difference between revisions

m
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 73:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F indexOf(haystack, needle)
V idx = 0
L(straw) haystack
Line 155:
j++
print()
i++</langsyntaxhighlight>
 
{{out}}
Line 241:
=={{header|Ada}}==
{{trans|Python}}
<langsyntaxhighlight Adalang="ada">with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
procedure Fraction_Reduction is
Line 360:
end loop;
end Fraction_Reduction;</langsyntaxhighlight>
 
=={{header|C}}==
{{trans|C#}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 500:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 583:
=={{header|C sharp|C#}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace FractionReduction {
Line 676:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 759:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <array>
#include <iomanip>
#include <iostream>
Line 864:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 947:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.range;
import std.stdio;
 
Line 1,037:
writeln;
}
}</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 1,122:
===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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,222:
}
fmt.Printf("Took %s\n", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 1,335:
{{trans|Phix}}
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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,454:
}
fmt.Printf("Took %s\n", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 1,524:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class FractionReduction {
static void main(String[] args) {
for (int size = 2; size <= 5; size++) {
Line 1,623:
return true
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (guard)
import Data.List (intersect, unfoldr, delete, nub, group, sort)
import Text.Printf (printf)
Line 1,680:
where
groups = [ findGroupReductions [10^1..99], findGroupReductions [10^2..999]
, findGroupReductions [10^3..9999], findGroupReductions [10^4..99999] ]</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by dropping 6
Line 1,763:
=={{header|J}}==
The algorithm generates all potential rational fractions of given size in base 10 and successively applies conditions to restrict the candidates. By avoiding boxing and rational numbers this version is much quicker than that which may be found in the page history.
<syntaxhighlight lang="j">
<lang J>
Filter=: (#~`)(`:6)
assert 'ac' -: 1 0 1"_ Filter 'abc'
Line 1,818:
result
)
</syntaxhighlight>
</lang>
<pre>
Line 1,873:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
Line 1,982:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,067:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
toi(set) = parse(Int, join(set, ""))
Line 2,110:
 
testfractionreduction()
</langsyntaxhighlight>{{out}}
<pre>
For 2 digits, there were 4 fractions with anomalous cancellation.
Line 2,194:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">fun indexOf(n: Int, s: IntArray): Int {
for (i_j in s.withIndex()) {
if (n == i_j.value) {
Line 2,297:
println()
}
}</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 2,380:
=={{header|Lua}}==
{{trans|C++}}
<langsyntaxhighlight lang="lua">function indexOf(haystack, needle)
for idx,straw in pairs(haystack) do
if straw == needle then
Line 2,482:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 2,564:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[AnomalousCancellationQ2]
AnomalousCancellationQ2[frac : {i_?Positive, j_?Positive}] :=
Module[{samedigits, idig, jdig, ff, p, q, r, tmp},
Line 2,626:
t = Take[sel, UpTo[12]];
Column[Row[{#3, "/", #4, " = ", #1, "/", #2, " by removing ", #5}] & @@@ t]
SortBy[Tally[sel[[All, -1]]], First]</langsyntaxhighlight>
{{out}}
<pre>4
Line 2,667:
=={{header|MiniZinc}}==
===The Model===
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Fraction Reduction. Nigel Galloway, September 5th., 2019
include "alldifferent.mzn"; include "member.mzn";
Line 2,683:
 
output [show(n)++"/"++show(i)++" becomes "++show(e)++"/"++show(g)++" when "++show(w)++" is omitted"]
</syntaxhighlight>
</lang>
===The Tasks===
;Displaying 12 solutions
Line 2,831:
{{trans|Phix}}
Using Phix algorithm with some adaptations.
<syntaxhighlight lang="nim">
<lang Nim>
# Fraction reduction.
 
Line 2,905:
echo &"{n}-digit fractions found: {count}, omitted {omitted}\n"
echo &"Took {gettime() - start}"
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,978:
Using a permutation k out of n with k <= n<BR>
Inserting a record with 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.
<langsyntaxhighlight lang="pascal">
program FracRedu;
{$IFDEF FPC}
Line 3,224:
writeln;
end;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,383:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 3,420:
printf " %s => %s removed %s\n", substr($f,0,$digit*2+1), $reduced{$f}, substr($f,-1)
}
}</langsyntaxhighlight>
{{out}}
<pre>4 2-digit reducible fractions:
Line 3,481:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_n</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">remove_digit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
Line 3,564:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,634:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def indexOf(haystack, needle):
idx = 0
for straw in haystack:
Line 3,720:
return None
 
main()</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 3,805:
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.
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require racket/generator
Line 3,864:
(stats 5))
 
(main)</langsyntaxhighlight>
 
{{out}}
Line 3,952:
{{works with|Rakudo|2019.07.1}}
;[[wp:Anomalous cancellation|Anomalous Cancellation]]
<syntaxhighlight lang="raku" perl6line>my %reduced;
my $digits = 2..4;
 
Line 3,995:
say " {.key.substr(0, $digit * 2 + 1)} => {.value} removed {.key.substr(* - 1)}"
for %d.pairs.pick(12).sort;
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>(4) 2 digit reduceable fractions:
Line 4,056:
 
=={{header|REXX}}==
<langsyntaxhighlight 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.*/
Line 4,098:
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 5 &nbsp; 12 </tt>}}
<pre>
Line 4,186:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight Rubylang="ruby">def indexOf(haystack, needle)
idx = 0
for straw in haystack
Line 4,293:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 4,376:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function IndexOf(n As Integer, s As Integer()) As Integer
Line 4,471:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>16/64 = 1/4 by omitting 6's
Line 4,559:
 
Have still needed to restrict to 5-digit fractions which finishes in just under 2 minutes on my machine.
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Struct
import "./fmt" for Fmt
 
var Result = Struct.create("Result", ["n", "nine"])
Line 4,645:
}
Fmt.print("$d-digit fractions found:$d, omitted $s\n", n, count, omitted)
}</langsyntaxhighlight>
 
{{out}}
Line 4,700:
=={{header|zkl}}==
{{trans|Phix}}
<langsyntaxhighlight lang="zkl">fcn toInt(digits,remove_digit=0){
if(remove_digit!=0) digits=digits.copy().del(digits.index(remove_digit));
digits.reduce(fcn(s,d){ s*10 + d });
Line 4,758:
println("%d-digit fractions found: %d, omitted %s\n"
.fmt(n,count,omitted.concat(",")));
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits