ONLY DO WHAT ONLY YOU CAN DO

こけたら立ちなはれ 立ったら歩きなはれ

VBScript で Project Euler Problem 4

昨日の続き
4桁の回文は

で、11の倍数だから、

 999897969594939291
99980197029603950494059306920791089009
98 9604950694089310921291149016 
97  94099312921591189021  
96   921691209024   
95    9025    
94         
93         
92         
91         

Option Explicit

Private loop_cnt: loop_cnt = 0
Private cnt:      cnt      = 0

Call main

WScript.Echo "ループ回数      = " & CStr(loop_cnt)
WScript.Echo "回文数 判定回数 = " & CStr(cnt)

Private Sub main()
    Dim i
    For i = 99 To 90 Step -1
        Dim x: x = i
        Dim y: y = i
        Do
            loop_cnt = loop_cnt + 1

            Dim n: n = x * y
            Dim s: s = CStr(n)
            WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s

            cnt = cnt + 1
            If s = StrReverse(s) Then
                Exit Sub
            End If

            x = x + 1
            if x > 99 Then Exit Do
            y = y - 1
        Loop
        WScript.Echo ""

        x = i
        y = i - 1
        Do
            loop_cnt = loop_cnt + 1

            n = x * y
            s = CStr(n)
            WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s

            cnt = cnt + 1
            If s = StrReverse(s) Then
                Exit Sub
            End If

            x = x + 1
            if x > 99 Then Exit Do
            y = y - 1
        Loop
        WScript.Echo ""
    Next
End Sub
D:\Project Euler\Problem 004>cscript //nologo 006.vbs
99 * 99 = 9801

99 * 98 = 9702

98 * 98 = 9604
99 * 97 = 9603

98 * 97 = 9506
99 * 96 = 9504

97 * 97 = 9409
98 * 96 = 9408
99 * 95 = 9405

97 * 96 = 9312
98 * 95 = 9310
99 * 94 = 9306

96 * 96 = 9216
97 * 95 = 9215
98 * 94 = 9212
99 * 93 = 9207

96 * 95 = 9120
97 * 94 = 9118
98 * 93 = 9114
99 * 92 = 9108

95 * 95 = 9025
96 * 94 = 9024
97 * 93 = 9021
98 * 92 = 9016
99 * 91 = 9009
ループ回数      = 25
回文数 判定回数 = 25

のうち、
99 * n
以外のものは、11の倍数にならないので計算する必要がない。

Option Explicit

Private loop_cnt: loop_cnt = 0
Private cnt:      cnt      = 0

Call main

WScript.Echo "ループ回数      = " & CStr(loop_cnt)
WScript.Echo "回文数 判定回数 = " & CStr(cnt)

Private Sub main()
    Dim i
    For i = 99 To 90 Step -1
        Dim x: x = i
        Dim y: y = i
        Do
            loop_cnt = loop_cnt + 1

            If (x mod 11 = 0) Or (y mod 11 = 0) Then
                Dim n: n = x * y
                Dim s: s = CStr(n)
                WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s

                cnt = cnt + 1
                If s = StrReverse(s) Then
                    Exit Sub
                End If
            End If

            x = x + 1
            if x > 99 Then Exit Do
            y = y - 1
        Loop

        x = i
        y = i - 1
        Do
            loop_cnt = loop_cnt + 1

            If (x mod 11 = 0) Or (y mod 11 = 0) Then
                n = x * y
                s = CStr(n)
                WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s

                cnt = cnt + 1
                If s = StrReverse(s) Then
                    Exit Sub
                End If
            End If

            x = x + 1
            if x > 99 Then Exit Do
            y = y - 1
        Loop
    Next
End Sub
D:\Project Euler\Problem 004>cscript //nologo 007.vbs
99 * 99 = 9801
99 * 98 = 9702
99 * 97 = 9603
99 * 96 = 9504
99 * 95 = 9405
99 * 94 = 9306
99 * 93 = 9207
99 * 92 = 9108
99 * 91 = 9009
ループ回数      = 25
回文数 判定回数 = 9

 999897969594939291
99980197029603950494059306920791089009
同様に6桁の回文も

で、11の倍数だから

Option Explicit

Private loop_cnt: loop_cnt = 0
Private cnt:      cnt      = 0

Call main

WScript.Echo "ループ回数      = " & CStr(loop_cnt)
WScript.Echo "回文数 判定回数 = " & CStr(cnt)

Private Sub main()
    Dim i
    For i = 999 To 900 Step -1
        Dim x: x = i
        Dim y: y = i
        Do
            loop_cnt = loop_cnt + 1

            If (x mod 11 = 0) Or (y mod 11 = 0) Then
                Dim n: n = x * y
                Dim s: s = CStr(n)

                cnt = cnt + 1
                If s = StrReverse(s) Then
                    WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s
                    Exit Sub
                End If
            End If

            x = x + 1
            if x > 999 Then Exit Do
            y = y - 1
        Loop

        x = i
        y = i - 1
        Do
            loop_cnt = loop_cnt + 1

            If (x mod 11 = 0) Or (y mod 11 = 0) Then
                n = x * y
                s = CStr(n)

                cnt = cnt + 1
                If s = StrReverse(s) Then
                    WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s
                    Exit Sub
                End If
            End If

            x = x + 1
            if x > 999 Then Exit Do
            y = y - 1
        Loop
    Next
End Sub
D:\Project Euler\Problem 004>cscript //nologo 008.vbs
993 * 913 = 906609
ループ回数      = 2203
回文数 判定回数 = 352

ロジックは一緒だけど、ソースを少し整理した

Option Explicit

Private loop_cnt: loop_cnt = 0
Private cnt:      cnt      = 0

Call main

WScript.Echo "ループ回数      = " & CStr(loop_cnt)
WScript.Echo "回文数 判定回数 = " & CStr(cnt)

Private Sub main()
    Dim i, j
    For i = 999 To 100 Step -1
        For j = 0 To 1
            Dim x: x = i
            Dim y: y = i - j
            Do
                loop_cnt = loop_cnt + 1

                If (x mod 11 = 0) Or (y mod 11 = 0) Then
                    cnt = cnt + 1

                    Dim s: s = CStr(x * y)
                    If s = StrReverse(s) Then
                        WScript.Echo CStr(x) & " * " & CStr(y) & " = " & s
                        Exit Sub
                    End If
                End If

                x = x + 1
                y = y - 1
            Loop Until (x > 999)
        Next
    Next
End Sub
D:\Project Euler\Problem 004>cscript //nologo 009.vbs
993 * 913 = 906609
ループ回数      = 2203
回文数 判定回数 = 352