算法(算法的综合知识)
来源:学生作业帮 编辑:神马作文网作业帮 分类:数学作业 时间:2024/11/13 03:30:25
设计一个算法求小于1000的所有质数
解题思路: 本着质数的含义:仅能被1和它本身整除的数!
解题过程:
Sub test()
Dim data(2 To 1000) As Long, temp As Long, i As Long, j As Long
For i = 4 To 1000 Step 2
data(i) = 1
Next i
temp = 3
While temp * temp < 1000
For i = temp * temp To 1000 Step temp * 2
data(i) = 1
Next i
temp = temp + 2
While data(temp) = 1
temp = temp + 2
Wend
Wend
Application.ScreenUpdating = False
With Sheet1
.Range("A:A").ClearContents
.Cells(1, 1) = 2
j = 2
For i = 3 To 1000 Step 2
If data(i) = 0 Then
.Cells(j, 1) = i
j = j + 1
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
最终答案:略
解题过程:
Sub test()
Dim data(2 To 1000) As Long, temp As Long, i As Long, j As Long
For i = 4 To 1000 Step 2
data(i) = 1
Next i
temp = 3
While temp * temp < 1000
For i = temp * temp To 1000 Step temp * 2
data(i) = 1
Next i
temp = temp + 2
While data(temp) = 1
temp = temp + 2
Wend
Wend
Application.ScreenUpdating = False
With Sheet1
.Range("A:A").ClearContents
.Cells(1, 1) = 2
j = 2
For i = 3 To 1000 Step 2
If data(i) = 0 Then
.Cells(j, 1) = i
j = j + 1
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
最终答案:略