中易网

哥德巴赫 VB.net 双重循环

答案:1  悬赏:30  
解决时间 2021-01-11 18:42
  • 提问者网友:放下
  • 2021-01-10 17:48
哥德巴赫 VB.net 双重循环
最佳答案
  • 二级知识专家网友:傲气稳了全场
  • 2021-01-10 18:54
Module Program
Sub Main()
Dim l,n1,n2 As Long

Console.Write("请输入一个大于等于6的偶数:")
l=CLng(Console.ReadLine())
For n1=3 To CLng(l/2)+1
n2=l-n1
If IsPrime(n1) AndAlso IsPrime(n2) Then
Exit For
End If
Next
Console.WriteLine("{0}={1}+{2}",l,n1,n2)
Console.Write("Press any key to continue . . . ")
Console.ReadKey(True)
End Sub
Function IsPrime(l As Long) As Boolean
Dim r As Boolean=False
Dim i As Long,sq As Long=CLng(Math.Sqrt(l))

For i=2 To sq
If 0=l Mod i Then
Exit For
End If
Next
If i>sq Then
r=True
End If
Return r
End Function
End Module
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息