如何用VB语言求水仙花数和求1~1000以内的素数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用VB语言求水仙花数和求1~1000以内的素数相关的知识,希望对你有一定的参考价值。
我和朋友一起发现了一个更简单的方法求1~1000以内的素数之和,VB代码如下:Private Sub Command1_Click()
Dim i, s, n, c As Double
i = 3: s = 0
Do While i <= 1000
n = 1: c = 0
Do While n <= i
If i Mod n = 0 Then
c = c + 1
End If
n = n + 1
Loop
If c = 2 Then
s = s + i
End If
i = i + 2
Loop
s = s + 2
Print s
End Sub
运行结果是对的,就是76127 参考技术A Private Sub Form1_Click()Dim a As Integer, b As Integer, c As IntegerFor a = 1 To 9For b = 0 To 9For c = 0 To 9If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c ThenForm1.Print a * 100 + b * 10 + cEnd IfNext cNext bNext aEnd 参考技术B '求水仙花数
Private Sub Form1_Click()
Dim a As Integer, b As Integer, c As Integer
For a = 1 To 9
For b = 0 To 9
For c = 0 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then
Form1.Print a * 100 + b * 10 + c
End If
Next c
Next b
Next a
End
'求1~1000以内的素数,
Private Sub Command1_Click()
Dim a As Integer, b As Integer
Text1 = ""
For a = 3 To 999 step 2
For b = 2 To Sqr(a)
If a Mod b = 0 Then
Exit For
End If
If b > Sqr(a) Then
Text1 = Text1 & a & VbNewline
End If
Next b
Next a
End追问
Text1 = ""是什么?
追答Text1是一个多行文本框,上面那一句是先清空其中的内容(以防多次运行结果太乱)。
以上是关于如何用VB语言求水仙花数和求1~1000以内的素数的主要内容,如果未能解决你的问题,请参考以下文章