急:验证歌德巴赫猜想:一个大偶数可以分解为两个素数之和。试编程将500-1000之间的全部偶数表示为两个素数之和

skytears 2003-12-24 04:51:10
验证歌德巴赫猜想:一个大偶数可以分解为两个素数之和。试编程将500-1000之间的全部偶数表示为两个素数之和
...全文
1594 点赞 收藏 17
写回复
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
skytears 2003-12-25
sorry,写错了,更正如下:
Function primeStatus(a As Integer) As Boolean
Dim i As Integer
Dim j As Integer

For i = 2 To a - 1
If a Mod i = 0 Then
primeStatus = False
Exit Function
End If
Next i
primeStatus = True

End Function

Private Sub Command1_Click()

Dim i As Integer
Dim j As Integer

For i = 500 To 1000 Step 2
For j = 1 To i - 1
If primeStatus(j) Then
If primeStatus(i - j) Then
Debug.Print i & " = " & j & " + " & (i - j)
End If
End If
Next j
Next i
End Sub
回复
skytears 2003-12-25
已经解决,谢谢各位
Function primeStatus(a As Integer) As Boolean
Dim i As Integer
Dim j As Integer

For i = 2 To a - 1
If a Mod i = 0 Then
primeStatus = False
Exit Function
End If
Next i
primeStatus = True

End Function

Private Sub Command1_Click()

Dim i As Integer
'Dim j As Integer

For i = 500 To 1000
If i Mod 2 = 0 Then
If primeStatus(i \ 2) Then
Print i;

End If
End If
Next i

End Sub

回复
captainivy 2003-12-24

其实也不难吗

有了判断素数的函数就好办了
回复
daisy8675 2003-12-24
要驗証了﹐就沒有所謂的猜想了。
回复
rainstormmaster 2003-12-24
同意楼上

一个判断数值是否为素数的Function:
Function PrimeStatus(TestVal As Long) As Boolean
Dim i As Long
Dim Lim As Integer
PrimeStatus = True
Lim = Sqr(TestVal)
For i = 2 To Lim
If TestVal Mod i = 0 Then
PrimeStatus = False
Exit For
End If
Next
End Function

如果要产生素数数组可以这样:
Function arrPrime1(ByVal num As Long) As Long()
Dim i As Long
Dim k As Long
Dim arrPrime() As Long
k = 0
For i = 2 To num
If PrimeStatus(i) = True Then
ReDim Preserve arrPrime(k)
arrPrime(k) = i
k = k + 1
End If
Next
arrPrime1 = arrPrime
End Function
回复
pigsanddogs 2003-12-24
从效率上看。 lazygod() 是最快的
不过找素数可以用筛选法,另外建立个500-1000的偶数数组。 然后将素数组合成偶数。
回复
lazygod 2003-12-24
把1000以下的素数找出来,保存到一个数组。看500-1000的数是由数组里的哪两个数之和
回复
liyan010 2003-12-24
告诉我你的email,我发给你个例子,以前作过的!
看了保你豁然开朗
回复
ljlfromjj 2003-12-24
只能被1和自己整除的就是素素。
回复
jhzhou882 2003-12-24
Private Sub Command5_Click()
Dim iloop As Integer
Dim jloop As Integer
Dim kloop As Integer
Dim flag1 As Boolean
Dim flag2 As Boolean
For iloop = 500 To 1000 Step 2
For jloop = 3 To iloop Step 2
flag1 = True
For kloop = 2 To jloop - 1

If (jloop Mod kloop) = 0 Then
flag1 = False
'Exit For
End If

Next kloop
If flag1 = True Then
flag2 = True
For kloop = 2 To iloop - jloop - 1
If ((iloop - jloop) Mod kloop) = 0 Then
flag2 = False
'Exit For
End If
Next kloop
If flag2 = True Then
Debug.Print jloop & " " & iloop - jloop
End If
End If
If flag2 = True Then
Exit For
End If
Next jloop
Next iloop
End Sub
回复
xuchuang 2003-12-24
Private Sub Command3_Click()
Dim i As Integer
Dim j As Integer
For i = 500 To 1000
For j = 3 To i / 2 Step 2
If s(j) = True Then
If s(i - j) = True Then
Debug.Print j, i - j
Exit For
End If
End If

Next
Next
End Sub

Private Function s(a As Integer) As Boolean
Dim i As Integer
Dim j As Integer
Dim k As Integer

If a = 3 Then
s = True
Exit Function
End If

If a = 2 Then
s = True
Exit Function
End If
k = a / 2
If (a Mod 2) = 0 Then
s = False
Exit Function
End If
For i = 3 To k Step 2
If (CInt(a / i)) * i = a Then
s = False
Exit Function
End If
Next
s = True
End Function
回复
skytears 2003-12-24
如何判断是素数
回复
xuchuang 2003-12-24
一个循环,查找比当前值小的素数,与当前值相减,判断差是否是素数,
回复
flyingscv 2003-12-24
for i =500 to 1000 step 2
for j= 3 to i step 2

if j 是素数 and i-j 是素数 then print i,j,i-j
回复
skytears 2003-12-24
时间关系给一下源程序或给一个思路。
回复
SnHnBn 2003-12-24
精屁极了
回复
victorycyz 2003-12-24
循环嵌套
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7451

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告