问:vb 验证哥德巴赫猜想 一次性显示6~100

2026年09月26日 17:30
有1个网友回答
网友(1):

Private Sub command1_click()
    Dim n1 As Long, n2 As Long
    For i = 6 To 100 Step 2
        For n1 = 2 To i / 2
            n2 = i - n1
            If IsPrime(n1) = True And IsPrime(n2) = True Then
                S = n1 & "+" & n2 & "=" & i
                Print S  
                '由此处输出在窗体上,你可以添加个list 
                '把结果放进列表里方便查询
                'List1.AddItem S  '比如这样
            End If
        Next
    Next
End Sub

Function IsPrime(b As Long) As Boolean
    Dim j As Long
    If IsNumeric(b) = False Or b < 2 Then
        IsPrime = False
        Exit Function
    Else
        For j = 2 To Sqr(b)
            If b Mod j = 0 Then
                IsPrime = False
                Exit Function
            End If
        Next j
        IsPrime = True
    End If
End Function