2019年1月27日日曜日

Juliaでパズル(4)

Juliaでパズル(3)からの続き)

目的はJuliaプログラミングの習得ということなのでこの話題がだらだたと続いている。高速が話題のJuliaで初めて実行時間が1時間以上のプログラムを作ったが,アルゴリズムがポンコツだといくらでも遅いプログラムができるのであった・・・orz

昨日の2項演算だけの組み合わせで整数を作るものから,単項演算を加えた組み合わせに拡張する。$5 \times 5^3 = 625$回から$5 \times 5^3 \times 5^7 ≒ 5000$万回に増えて7時間で終わらなかったので,次は時間短縮の方法を考えなければならない。

単項演算は1つのエントリーポイントで1回だけ作用するものとしている。
以下は,7重ループを5重ループに落として7分程度で実行したもの。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function sqr(x)
  return sqrt(abs(x))
end

function cbr(x)
  return cbrt(abs(x))
end

function rog(x)
  return log(abs(x))
end

function cc(a,b)
  if (a==1 && b==1) || (a==11 && b==1)
    return 10*a+b
  else
    return 0
  end
end

function ne(op, op1)
  expr = Expr(:call,Symbol(op), op1)
  return expr
end

function me(op, op1, op2)
  expr = Expr(:call,Symbol(op), op1, op2)
  return expr
end

function doe1(a,b,c,d)
# ((1,1)_a,(1,1)_c)_b
    x=me(a,ne(d[1],1),ne(d[2],1))
    y=me(c,ne(d[3],1),ne(d[4],1))
    z=ne(d[7],me(b,ne(d[5],x),ne(d[6],y)))
    if b=="cc"
      z=:0
    end
    return (z,eval(z))
end

function doe2(a,b,c,d)
# (((1,1)_a,1)_b,1)_c
    x=me(a,ne(d[1],1),ne(d[2],1))
    y=me(b,ne(d[3],x),ne(d[4],1))
    z=ne(d[7],me(c,ne(d[5],y),ne(d[6],1)))
    if  (a!="cc" && b=="cc") || ((a!="cc" || b!="cc") && c=="cc")
      z=:0
    end
    return (z,eval(z))
end

function doe3(a,b,c,d)
# ((1,(1,1)_b)_a,1)_c
    x=me(b,ne(d[1],1),ne(d[2],1))
    y=me(a,ne(d[3],1),ne(d[4],x))
    z=ne(d[7],me(c,ne(d[5],y),ne(d[6],1)))
    if a=="cc" || c=="cc"
      z=:0
    end
    return (z,eval(z))
end

function doe4(a,b,c,d)
# (1,((1,1)_b,1)_c)_a
    x=me(b,ne(d[1],1),ne(d[2],1))
    y=me(c,ne(d[3],x),ne(d[4],1))
    z=ne(d[7],me(a,ne(d[6],1),ne(d[5],y)))
    if a=="cc" || (b!="cc" && c=="cc")
      z=:0
    end
    return (z,eval(z))
end

function doe5(a,b,c,d)
# (1,(1,(1,1)_c)_b)_a
    x=me(c,ne(d[1],1),ne(d[2],1))
    y=me(b,ne(d[3],1),ne(d[4],x))
    z=ne(d[7],me(a,ne(d[6],1),ne(d[5],y)))
    if a=="cc" || b=="cc"
      z=:0
    end
    return (z,eval(z))
end

function main(pz)
  uno=["+","-","*","/","cc"]
  dos=["+","sqr","cbr","exp","rog"]
  d=["+","+","+","+","+","+","+"]
for a in uno, b in uno, c in uno
#a="*"; b="-"; c="/";
  for d[1] in dos, d[2] in dos, d[3] in dos, d[4] in dos, d[7] in dos
#, d[5] in dos, d[6] in dos, d[7] in dos
    e,f = doe1(a,b,c,d)
    if f>0.5 && f<200
      push!(pz,(e,f))
    end
    e,f = doe2(a,b,c,d)
    if f>0.5 && f<200
      push!(pz,(e,f))
    end
    e,f = doe3(a,b,c,d)
    if f>0.5 && f<200
      push!(pz,(e,f))
    end
    e,f = doe4(a,b,c,d)
    if f>0.5 && f<200
      push!(pz,(e,f))
    end
    e,f = doe5(a,b,c,d)
    if f>0.5 && f<200
      push!(pz,(e,f))
    end
  end
  end
end

function fine(pz)
  println(length(pz))
  qz=[]
  sort!(pz, by=x->x[2])
  pl=length(pz)
  for i in 1:pl
    push!(qz,Int(ceil(pz[i][2])))
  end

  for i in 1:121
    if in(i,qz)==true
      j=findfirst(isequal(i),qz)
      rz=repr(pz[j][1])
      rz=replace(rz,":"=>"")
      rz=replace(rz,"cc(+1, +1)"=>"11")
      rz=replace(rz,"cc(+(11), +1)"=>"111")
      rz=replace(rz,"+1"=>"1")
      println(i," -> ",rz," = ",pz[j][2])
    end
  end
end

pz=[]
@time main(pz)
@time fine(pz)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
408.936226 seconds (144.22 M allocations: 8.591 GiB, 5.96% gc time)
751191
1 -> (exp(+((rog(1) - rog(exp(1) + exp(1)))) + 1)) = 0.5000000000000001
2 -> (cbr(+(1 / exp(11)) + 1)) = 1.000005567202603
3 -> (cbr(+(exp(1) * sqr(11)) - 1)) = 2.0012925727345556
4 -> (+((+((exp(1) - cbr(1 / exp(1)))) + 1))) = 3.001750517885256
5 -> (exp(+((exp(1) - sqr(exp(1) + exp(1)))) + 1)) = 4.0013741789600505
6 -> (+(+(exp(1) / rog(1 - exp(1))) * 1)) = 5.021535230269061
7 -> (exp(+(1 / cbr(1 + 1)) + 1)) = 6.011657650957131
8 -> (exp(+((1 + cbr(exp(1) * exp(1)))) - 1)) = 7.012778894114402
9 -> (+((+(exp(1) * sqr(11)) - 1))) = 8.015520899439874
10 -> (exp(+((rog(1) + cbr(1 - exp(1)))) + 1)) = 9.004695764350682
11 -> (rog(+(exp(11) / exp(1)) + 1)) = 10.000045398899218
12 -> (exp(+((1 + rog(11))) - 1)) = 11.000000000000002
13 -> (rog(+(exp(1) * exp(11)) + 1)) = 12.000006144193478
14 -> (exp(+(rog(1 + exp(1)) * exp(1)) - 1)) = 13.063412466658708
15 -> (exp(+((sqr(exp(1) + rog(1)) + rog(1))) + 1)) = 14.135951028495938
16 -> (sqr(+((exp(1) - exp(exp(1) + exp(1)))) + 1)) = 15.031080541832813
17 -> (+((+(exp(1) / exp(1 - exp(1))) + 1))) = 16.15426224147926
18 -> (+((+((1 + exp(exp(1) + rog(1)))) + 1))) = 17.154262241479262
19 -> (exp(+((exp(1) + exp(1 - exp(1)))) * 1)) = 18.131593378402822
20 -> (exp(+((rog(1) + cbr(exp(1) * exp(1)))) + 1)) = 19.062709434872296
21 -> (+(+((exp(1) + rog(1))) * +(exp(1) * exp(1)))) = 20.085536923187664
22 -> (exp(+(+(11) / exp(1)) - 1)) = 21.045228353448717
23 -> (exp(+((exp(1) - sqr(1 / exp(1)))) + 1)) = 22.46034183113636
24 -> (exp(+(exp(1) / cbr(1 + 1)) + 1)) = 23.511783406204675
25 -> (sqr(+(exp(exp(1) * exp(1)) / exp(1)) - 1)) = 24.37815447036041
26 -> (sqr(+(exp(exp(1) + exp(1)) * exp(1)) + 1)) = 25.005158374895853
27 -> (exp(+((exp(1) + rog(1 - exp(1)))) * 1)) = 26.039293433236857
28 -> (exp(+((1 + sqr(1 - exp(1)))) + 1)) = 27.40793292860369
29 -> (cbr(+(exp(11) / exp(1)) - 1)) = 28.031200676839138
30 -> (+(+((exp(1) + exp(1))) * +((exp(1) + exp(1))))) = 29.556224395722598
31 -> (exp(+((exp(1) + rog(exp(1) + exp(1)))) - 1)) = 30.308524482958514
32 -> (exp(+((exp(1) + cbr(1 / exp(1)))) * 1)) = 31.025614547492058
33 -> (exp(+((cbr(exp(1) + exp(1)) + exp(1))) - 1)) = 32.350947201581
35 -> (exp(+((exp(1) - exp(1 - exp(1)))) + 1)) = 34.42929324111139
36 -> (exp(+(sqr(1 - exp(1)) * exp(1)) * 1)) = 35.27632820034533
37 -> (exp(+(rog(exp(1) + exp(1)) * exp(1)) - 1)) = 36.68805458104296
38 -> (exp(+(11) - +(exp(1) * exp(1)))) = 37.00096158422464
39 -> (exp(+((sqr(1 + exp(1)) + exp(1))) - 1)) = 38.34279034771416
40 -> (exp(+((exp(1) + cbr(exp(1) * exp(1)))) - 1)) = 39.09583226076508
41 -> (+(+((exp(1) + exp(1))) * +(exp(1) * exp(1)))) = 40.17107384637533
42 -> (exp(+((+((1 + 1)) + exp(1))) - 1)) = 41.1935556747161
43 -> (+((+((exp(1 + exp(1)) + rog(1))) + 1))) = 42.193555674716116
44 -> (+((+((1 + exp(1 + exp(1)))) + 1))) = 43.193555674716116
45 -> (exp(+((exp(1) + exp(rog(1) - exp(1)))) + 1)) = 44.00353027860415
47 -> (exp(+(sqr(1 + 1) * exp(1)) * 1)) = 46.72274206040535
48 -> (exp(+((exp(exp(1) - 1) - exp(1))) + 1)) = 47.30706718593521
50 -> (exp(+((exp(1) + exp(1 - exp(1)))) + 1)) = 49.286780801520734
51 -> (exp(+((exp(1) + cbr(1 - exp(1)))) * 1)) = 50.20065233451701
52 -> (exp(+((exp(1) + cbr(11))) - 1)) = 51.5350376483723
54 -> (exp(+((cbr(1 + 1) + exp(1))) * 1)) = 53.42094397564407
55 -> (cbr(+(exp(1) * exp(11)) - 1)) = 54.598038212039256
56 -> (exp(+(exp(1) / rog(1 - exp(1))) - 1)) = 55.78668552594455
57 -> (exp(+((exp(1) + sqr(1 - exp(1)))) * 1)) = 56.21110430560197
58 -> (exp(+(1 / exp(1)) * +(11))) = 57.20686180895067
60 -> (exp(+((exp(1) + exp(rog(1) - 1))) + 1)) = 59.511005963978846
62 -> (exp(+((cbr(exp(1) + rog(1)) + exp(1))) * 1)) = 61.18452227596258
63 -> (exp(+((sqr(1 + 1) + exp(1))) * 1)) = 62.33327490494041
65 -> (exp(+((exp(1) + exp(1 / exp(1)))) * 1)) = 64.2607927021827
67 -> (sqr(+(exp(exp(1) * exp(1)) * exp(1)) - 1)) = 66.31488392984271
68 -> (exp(+(cbr(1 + exp(1)) * exp(1)) * 1)) = 67.43919204479707
69 -> (exp(+((1 + cbr(11))) + 1)) = 68.30480329758417
70 -> (exp(+(sqr(1 + exp(1)) * exp(1)) - 1)) = 69.52046855397886
71 -> (exp(+(cbr(1 - exp(1)) * exp(1)) + 1)) = 70.51403099154743
72 -> (exp(+((cbr(1 + exp(1)) + exp(1))) * 1)) = 71.34344142876819
74 -> (exp(+(cbr(exp(1) * exp(1)) * exp(1)) - 1)) = 73.2948282500825
75 -> (exp(+((rog(1) + sqr(11))) + 1)) = 74.93527870314952
76 -> (exp(+((exp(1) + sqr(1 / exp(1)))) + 1)) = 75.55134476063684
77 -> (exp(+((1 + sqr(exp(1) + exp(1)))) + 1)) = 76.06924026135655
79 -> (exp(+((sqr(exp(1) + rog(1)) + exp(1))) * 1)) = 78.80710038074831
82 -> (+(+(exp(1) * exp(1)) * +(11))) = 81.27961708823715
83 -> (exp(+((exp(1) + rog(1 + 1))) + 1)) = 82.3871113494322
84 -> (+((+(exp(exp(1) + exp(1)) / exp(1)) - 1))) = 83.48412584713863
85 -> (exp(+((exp(1) + cbr(1 / exp(1)))) + 1)) = 84.33636424122227
86 -> (+((+(exp(exp(1) + exp(1)) / exp(1)) + 1))) = 85.48412584713863
88 -> (exp(+((cbr(exp(1) + exp(1)) + exp(1))) * 1)) = 87.93899191149563
89 -> (exp(+(sqr(exp(1) + rog(1)) * exp(1)) * 1)) = 88.38383317988601
96 -> (exp(+(sqr(1 - exp(1)) * exp(1)) + 1)) = 95.89100192175613
97 -> (exp(+(rog(1 + exp(1)) * exp(1)) + 1)) = 96.52628755961122
98 -> (exp(+(1 / exp(1 - exp(1))) - 1)) = 97.02236556502673
100 -> (exp(+(rog(exp(1) + exp(1)) * exp(1)) * 1)) = 99.72847208916271
105 -> (exp(+((sqr(1 + exp(1)) + exp(1))) * 1)) = 104.22651025460627
107 -> (exp(+((exp(1) + cbr(exp(1) * exp(1)))) * 1)) = 106.27349040292063
110 -> (+((+(cc(+(11), 1)) - 1))) = 110
111 -> (+((+(exp(1 + exp(1)) * exp(1)) - 1))) = 110.9756938401968
112 -> (exp(+((1 + 1)) + +((exp(1) + rog(1))))) = 111.97569384019675
113 -> (+((+(exp(1 + exp(1)) * exp(1)) + 1))) = 112.9756938401968
120 -> (exp(+(cbr(exp(1) + exp(1)) * exp(1)) * 1)) = 119.07124802191112
121 -> (exp(+(cbr(exp(1) + rog(1)) * exp(1)) + 1)) = 120.74343163832631
  6.644247 seconds (64.72 M allocations: 1004.683 MiB, 1.93% gc time)


0 件のコメント:

コメントを投稿