3元１次不定方程式〈1/2〉「2018東工大数学第2問」WolframAlphaとvbaソルバーでやってみたい。


オリジナル<東京工大数学 過去問ライブラリー

```
(1)35*x + 91*y + 65*z = 3　整数
(2)x**2+y**2の最小値
```

https://www.densu.jp/tokyokogyo/18tokyokogyopass.pdf#page=2


https://mathexamtest.web.fc2.com/2018/201810267/2018102670100mj.html#top-0102

# 解答

https://www.densu.jp/tokyokogyo/18tokyokogyopass.pdf#page=8

https://www.densu.jp/frtokyokogyo.htm

# wolframalpha で

①教えて下さい。(2) x^2+y^2の最小値について、
Integer solutionをどのように使ったらいいかわかりませんでした。
②over the integers　の使い方のwebページを教えて下さい。

### (1) x,y,z

https://www.wolframalpha.com/input?i=35*x+%2B+91*y+%2B+65*z+%3D+3%E3%80%80%E3%81%AE%E3%80%80%E6%95%B4%E6%95%B0%E8%A7%A3

上と同じです。

https://www.wolframalpha.com/input?i=35*x+%2B+91*y+%2B+65*z+%3D+3

Integer solution

x = 13 n_1 + 9, y = 5 n_2 + 3, z = -7 n_1 - 7 n_2 - 9, n_2 element Z, n_1 element Z


https://www.wolframalpha.com/input?i=35*x+%2B+91*y+%2B+65*z+%3D+3%E3%80%80over+the+integers

x = -13 c_1 - 13 c_2 - 17 and y = 5 c_1 + 3 and z = 7 c_2 + 5 and c_1 element Z and c_2 element Z


### (2) x^2+y^2の最小値

https://www.wolframalpha.com/input?i=x+%3D+13+n_1+%2B+9%2C+y+%3D+5+n_2+%2B+3%2Cx%5E2%2By%5E2

????????　

# vbaでループを繰り返す。

(省略)
(20220414) 〈2/2〉で、python ループでやってみました。


# vbaソルバーで

①いい方法を教えて下さい。短くなりますか？
以下のmySolver2は、mySolver1を利用していません。独立しています。
②SolverAddで、あやしいですね。
③vbaでInteger solutionの計算ができますか？

```vb
Const myMethod = "GRG Nonlinear"
Const myFormula1 = "35*x + 91*y + 65*z - 3"     '(9,-2,-2)
Const myFormula2 = "x^2 + y^2"                  '(-4,-2,5) Minimize 20
Function xyz2cell(f As String)
    Dim g As String
    g = f
    g = Replace(g, "x", "RC[-3]")
    g = Replace(g, "y", "RC[-2]")
    g = Replace(g, "z", "RC[-1]")
    xyz2cell = "=" & g
End Function
Function xyz2cell2(f As String)
    Dim g As String
    g = f
    g = Replace(g, "x", "RC[-4]")
    g = Replace(g, "y", "RC[-3]")
    xyz2cell2 = "=" & g
End Function
Sub mySolver1A(iR, myFormula As String)
    Cells(iR, 4).Formula = xyz2cell(myFormula)
    Dim ws As Worksheet: Set ws = ActiveSheet
    SolverReset
    SolverOk setCell:=ws.Cells(iR, 4), _
                   MaxMinVal:=3, _
                   ByChange:=ws.Range(Cells(iR, 1), Cells(iR, 3)), _
                   EngineDesc:=myMethod
    SolverAdd cellRef:=Range(Cells(iR, 1), Cells(iR, 3)).Address, relation:=4
    SolverAdd cellRef:=Range(Cells(iR, 1), Cells(iR, 3)), relation:=3, formulaText:=-10
    SolverOptions AssumeNonNeg:=False
    SolverSolve UserFinish:=True
End Sub
Sub mySolver1B(iR, myFormula As String)
    Cells(iR, 4).Formula = xyz2cell(myFormula)
    Dim ws As Worksheet: Set ws = ActiveSheet
    SolverReset
    SolverOk setCell:=ws.Cells(iR, 4), _
                   MaxMinVal:=3, _
                   ByChange:=ws.Range(Cells(iR, 1), Cells(iR, 3)), _
                   EngineDesc:=myMethod
    SolverAdd cellRef:=Range(Cells(iR, 1), Cells(iR, 3)).Address, relation:=4
    SolverAdd cellRef:=Range(Cells(iR, 1), Cells(iR, 3)), relation:=3, formulaText:=-4
    SolverAdd cellRef:=Cells(iR, 1), relation:=2, formulaText:=9
    SolverOptions AssumeNonNeg:=False
    SolverSolve UserFinish:=True
End Sub
Sub mySolver2(iR, myFormula1 As String, myFormula2 As String)
    Cells(iR, 4).Formula = xyz2cell(myFormula1)
    Cells(iR, 5).Formula = xyz2cell2(myFormula2)
    Dim ws As Worksheet: Set ws = ActiveSheet
    SolverReset
    SolverOk setCell:=ws.Cells(iR, 5), _
                   MaxMinVal:=2, _
                   ByChange:=ws.Range(Cells(iR, 1), Cells(iR, 3)), _
                   EngineDesc:=myMethod
    SolverAdd cellRef:=Range(Cells(iR, 1), Cells(iR, 3)).Address, relation:=4
    SolverAdd cellRef:=Range(Cells(iR, 1), Cells(iR, 3)).Address, relation:=3, formulaText:=-10
    SolverAdd cellRef:=Cells(iR, 4), relation:=2, formulaText:=0
    SolverOptions AssumeNonNeg:=False
    SolverSolve UserFinish:=True
End Sub
Sub aaa_main()
    ActiveSheet.Cells.Clear
    iR = 1
    Call mySolver1A(iR, myFormula1)
    iR = iR + 1
    Call mySolver1B(iR, myFormula1)
    iR = iR + 1
    Call mySolver2(iR, myFormula1, myFormula2)
End Sub
'-4  -7  12  0
' 9  -2  -2  0
'-4  -2   5  0  20

```

# つづく　＜2/2＞　へ