So long as external libraries are permitted:
use Algorithm::Permute"permute";
permute{for$x(@a=qw(+ - / *)){for$y(@a){$_="@ARGV";s/ /$x/;s/ /$y/;printf"
$_ = %.0f",eval}}}@ARGV
2nd newline is significant.
Without a module, and assuming that all three inputs are distinct, here's another solution:
@n=& ARGV;
@o=( q[+],
"-", q{/}, '*' );;
for$ {a}(@ n){ for
$b(@n){for$c(@ {n}){ for $x(
@o){for$y(@o){ ($a-$ b)*($a-$c)* ($b-$
c)||next;$_=$a .$x.$ b."$y$c";$% =42
/84+ eval; print"",$_, "$S="
,$S, $%,$/ }}} }};
;sub ARGV{ $S= $".
"";@ ARGV} ;1+ 2+3
Has also oneliners, luckily we have Eclipse and Netbeans automatic code-format! :-) Also implemented parenthesis (but also contains trivial ops)?
public class CodeGolf{static String[]o={"+","-","/","*"};static void p(N a,int b,N c,int d,N e,int i){System.out.printf("%s%s(%s%s%s) = %s\n",a,o[b],c,o[d],e,new N(a,b,new N(c,d,e)));}public static void main(String[]v){N[]n={new N(v[0]),new N(v[1]),new N(v[2])};for(int i=0,j=0,k=0,l=0,m=0;m<3;i++,j+=i==4?1:0,i%=4,k+=j==4?1:0,j%=4,l+=k==3?1:0,k%=3,m+=l==3?1:0,l%=3){p(n[k],i,n[l],j,n[m],0);}}static class N{Double v;N(String s){v=v.parseDouble(s);}N(N a,int o,N b){if(a.v==null||b.v==null)return;double x=b.v, y=a.v; switch(o){case 0:x=-x;case 1:v=y-x;return;case 3:v=y*x;x=0;case 2:if(x!=0)v=y/x;}}public String toString(){return v!=null?""+Math.round(v):"NaN";}}}
Expanded, formatted, version with comments:
public class CodeGolf {
// operators
static String[] o = {"+", "-", "/", "*"};
// print
static void p(N a, int b, N c, int d, N e, int i) {
System.out.printf("%s%s(%s%s%s) = %s\n", a, o[b], c, o[d], e,
new N(a, b, new N(c, d, e))); // calculate
}
public static void main(String[] v) {
N[] n = {new N(v[0]), new N(v[1]), new N(v[2])};
// Nested for-loops? Nah, too much code!
// Conditional operator, modulus is way cooler.
for (int i = 0, j = 0,
k = 0, l = 0, m = 0; m < 3; i++,
j += i == 4 ? 1 : 0,
i %= 4,
k += j == 4 ? 1 : 0,
j %= 4,
l += k == 3 ? 1 : 0,
k %= 3,
m += l == 3 ? 1 : 0,
l %= 3) {
p(n[k], i, n[l], j, n[m], 0);
}
}
// number wrapper
static class N {
Double v;
// parse input
N(String s) {
v = v.parseDouble(s);
}
// calculate input
N(N a, int o, N b) {
// NaN's should fall through
if (a.v == null || b.v == null) {
return;
}
double x = b.v, y = a.v;
// operator execution
switch (o) {
case 0:
x = -x;
// fall through; y + x = y - (-x)
case 1:
v = y - x;
return; // break would make it 665 characters, not as cool
case 3:
v = y * x;
x = 0;
// fall through; no return needed
case 2:
if (x != 0) {
v = y / x;
}
// will NaN because v = null if x = 0
}
}
// rounding and NaN
public String toString() {
return v != null ? "" + Math.round(v) : "NaN";
}
}
}
Iterate on both operators (4 * 4) and permute on operands, twice (3! * 2) makes (4 * 4 * 3 * 2 * 2 = 192 possiblities).
+/- 2.5 hours :-) Enjoy!
Single Line Version (Original)
program p;{$APPTYPE CONSOLE}uses SysUtils;type g=Integer;function a(b,c:g):g;begin a:=b+c;end;function s(b,c:g):g;begin s:=b-c;end;function m(b,c:g):g;begin m:=b*c;end;function d(b,c:g):g;begin d:=b div c;end;type t=function(b,c:g):g;r=record f:t;c:char;p:boolean;end;procedure q(l:Array of g;w,e:r);var b:String;x,y,z:g;begin for x:=0 to 2 do for y:=0 to 2 do for z:=0 to 2 do if not((x=y)or(x=z)or(y=z))then begin try if(w.p)or not(w.p xor e.p)then b:=IntToStr(e.f(w.f(l[x],l[y]),l[z]))else b:=IntToStr(w.f(l[x],e.f(l[y],l[z])));except b:='NaN';end;writeln(l[x],w.c,l[y],e.c,l[z],'=',b);end;end;const O:Array[0..3]of r=((f:a;c:'+';p:false),(f:s;c:'-';p:false),(f:m;c:'*';p :true),(f:d;c:'/';p:true));var L:Array[0..2] of g;I,J:g; begin for I:=0 to 2 do L[I]:=StrToInt(ParamStr(I+1));for I:=0 to 3 do for J:=0 to 3 do q(l,o[I],o[J]);end.
Single Line Version (Shortened to 747 Characters)
program p;{$APPTYPE CONSOLE}uses SysUtils,Math;type g=integer;t=function(b,c:g):g;r=record f:t;p:boolean;end;function a(b,c:g):g;begin a:=b+c end;function s(b,c:g):g;begin s:=b-c end;function m(b,c:g):g;begin m:=b*c end;function d(b,c:g):g;begin d:=b div c end;const f=true;u=false;n=[1..4];b=[1..3];c='+-*/';O:Array[1..4]of r=((f:a;p:f),(f:s;p:f),(f:m;p:u),(f:d;p:u));var l: Array[1..3]of g;I,J,x,y,z:g;w,e:r;begin for I in b do l[I]:=StrToInt(ParamStr(I));for I in n do for J in n do for x in b do for y in b do for z in b do if not((x=y)or(x=z)or(y=z))then begin w:=O[I];e:=O[J];write(l[x],c[I],l[y],c[J],l[z],'=');try writeLn(ifthen(w.p or not(w.p xor e.p),e.f(w.f(l[x],l[y]),l[z]),w.f(l[x],e.f(l[y],l[z]))))except writeln('NaN')end;end;end.
Formated:
program p;
{$APPTYPE CONSOLE}
uses SysUtils;
type
g = Integer;
function a(b, c: g): g;
begin
a := b + c;
end;
function s(b, c: g): g;
begin
s := b - c;
end;
function m(b, c: g): g;
begin
m := b * c;
end;
function d(b, c: g): g;
begin
d := b div c;
end;
type
t = function(b, c: g): g;
r = record
f: t;
c: char;
p: boolean;
end;
procedure q(l: Array of g; w, e: r);
var
b: String;
x, y, z: g;
begin
for x := 0 to 2 do
for y := 0 to 2 do
for z := 0 to 2 do
if not((x = y) or (x = z) or (y = z)) then
begin
try
if (w.p) or not(w.p xor e.p) then
b := IntToStr(e.f(w.f(l[x], l[y]), l[z]))
else
b := IntToStr(w.f(l[x], e.f(l[y], l[z])));
except
b := 'NaN';
end;
writeln(l[x], w.c, l[y], e.c, l[z], '=', b);
end;
end;
const
O: Array [0..3] of r = ((f: a; c: '+'; p: false), (f: s; c: '-'; p: false),
(f: m; c: '*'; p: true), (f: d; c: '/'; p: true));
var
l: Array [0..2] of g;
I, J: g;
begin
for I := 0 to 2 do
l[I] := StrToInt(ParamStr(I + 1));
for I := 0 to 3 do
for J := 0 to 3 do
q(l, O[I], O[J]);
end.
This is by far the ugliest code I have ever written.
Outputs rational numbers, not integers.
(],"1'=',"1 ":@x:@".)((' ',>@{.),@,.":"0@>@{:)"1>{(,{;~'+-*%');<<"1(i.!3)A.
Old version which didn't permute the input (was 55 characters)
(],"1'=',"1 ":@x:@".)(>,{;~'+-*%')(' 'I.@:E.s)}"1 s=:":
Example (note that J's order of operations is right-to-left):
(],"1'=',"1 ":@x:@".)((' ',>@{.),@,.":"0@>@{:)"1>{(,{;~'+-*%');<<"1(i.!3)A.1 2 3
1+2+3=6
1+3+2=6
2+1+3=6
2+3+1=6
3+1+2=6
3+2+1=6
1+2-3=0
1+3-2=2
2+1-3=0
2+3-1=4
3+1-2=2
3+2-1=4
1+2*3=7
1+3*2=7
2+1*3=5
2+3*1=5
3+1*2=5
3+2*1=5
1+2%3=5r3
1+3%2=5r2
2+1%3=7r3
2+3%1=5
3+1%2=7r2
3+2%1=5
1-2+3=_4
1-3+2=_4
2-1+3=_2
2-3+1=_2
3-1+2=0
3-2+1=0
1-2-3=2
1-3-2=0
2-1-3=4
2-3-1=0
3-1-2=4
3-2-1=2
1-2*3=_5
1-3*2=_5
2-1*3=_1
2-3*1=_1
3-1*2=1
3-2*1=1
1-2%3=1r3
1-3%2=_1r2
2-1%3=5r3
2-3%1=_1
3-1%2=5r2
3-2%1=1
1*2+3=5
1*3+2=5
2*1+3=8
2*3+1=8
3*1+2=9
3*2+1=9
1*2-3=_1
1*3-2=1
2*1-3=_4
2*3-1=4
3*1-2=_3
3*2-1=3
1*2*3=6
1*3*2=6
2*1*3=6
2*3*1=6
3*1*2=6
3*2*1=6
1*2%3=2r3
1*3%2=3r2
2*1%3=2r3
2*3%1=6
3*1%2=3r2
3*2%1=6
1%2+3=1r5
1%3+2=1r5
2%1+3=1r2
2%3+1=1r2
3%1+2=1
3%2+1=1
1%2-3=_1
1%3-2=1
2%1-3=_1
2%3-1=1
3%1-2=_3
3%2-1=3
1%2*3=1r6
1%3*2=1r6
2%1*3=2r3
2%3*1=2r3
3%1*2=3r2
3%2*1=3r2
1%2%3=3r2
1%3%2=2r3
2%1%3=6
2%3%1=2r3
3%1%2=6
3%2%1=3r2
600 bytes on disk with DOS line endings.
#define C B a,B b
#define D(N,O)B N(C){return a O b;}
#define E(A,B,C)i=A;j=B;k=C;X(m,p)X(m,m)X(t,t)X(d,t)X(t,d)X(d,d)Y(m,p)Y(p,p)Y(p,t)Y(p,d)Y(m,t)Y(m,d)
#define U"%.0f"
#define P(S,T)printf(U Z(S)U Z(T)U"="U"\n",v[i],v[j],v[k],
#define p +
#define m -
#define t *
#define d /
#define X(S,T)P(S,T)f##S(f##T(v[i],v[j]),v[k]));
#define Y(S,T)P(S,T)f##S(v[i],f##T(v[j],v[k])));
#define Z(A)#A
typedef double B;D(fp,+)D(fm,-)D(ft,*)B fd(C){return b?(int)(a/b+.5):-0.0;}main(int i,char*b[]){int j,k;B v[3]={atoi(b[1]),atoi(b[2]),atoi(b[3])};E(0,1,2)E(0,2,1)E(1,0,2)E(1,2,0)E(2,0,1)E(2,1,0)}
C doesn't seem to have NaN literals, so you get -0 if there's anything wrong rather than that.
However I think it fits the bill otherwise. (Note that the data type is double
so that if it DID have a NaN in there, it will get printed out as such by printf
.)
(not counting unnecessary line breaks and indentation)
Edit: Now with input permutation
o=" ";i=i.split(o);z="+-*/";for(y=0;y<27;y++)for(x=0;x<16;x++){a=y/9|0;b=(y/3|0)%3;c=y%3;if(a!=b&&a!=c&&b!=c){s=i[a]+z[x/4|0]+i[b]+z[x%4]+i[c];o+=s+"="+~~(eval(s)+.5);}}
With indentation:
o=" ";
i=i.split(o);
z="+-*/";
for(y=0;y<27;y++)
for(x=0;x<16;x++)
{
a=y/9|0;
b=(y/3|0)%3;
c=y%3;
if(a!=b&&a!=c&&b!=c)
{
s=i[a]+z[x/4|0]+i[b]+z[x%4]+i[c];
o+=s+"="+~~(eval(s)+.5);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With