用 vb 做 100以内质数求和 的程序

[复制链接]
查看11 | 回复3 | 2009-6-26 11:09:25 | 显示全部楼层 |阅读模式
在窗体上添加个按钮Private Sub Command1_Click()Dim i As Integer, s As Integer, j As Integer, l As IntegerFor i = 2 To 100l = 0For j = 2 To i - 1If i Mod j = 0 Then l = 1Next jIf l1 Then s = s + iNext iPrint "100以内质数和为" & sEnd Sub
回复

使用道具 举报

千问 | 2009-6-26 11:09:25 | 显示全部楼层
Fuction IsSushu(I as integer) as booleanfor J as integer = 2 to cint(Sqrt(I)) ' 任意整数如果除以 2 到它的平方根均不能整除则为质数if I mod J = 0 then exit Functionnextreturn True ' 因为没有被整除过,因此返回 Trueend functionfor I as integer = 1 to 100if IsSushu(I)=true thenSum = sum + inext i身在外地,没有 Basic 测试,你试试吧
回复

使用道具 举报

千问 | 2009-6-26 11:09:25 | 显示全部楼层
Private Sub Command1_Click()For n = 1 To 100If IsPrime(n) = True Then Sum = Sum + nNextPrint SumEnd SubFunction IsPrime(ByVal n As Long) As BooleanDim i As LongFor i = 2 To Int(n / 2)
If n Mod i = 0 Then IsPrime = False: Exit FunctionNextIsPrime = TrueEnd Function
回复

使用道具 举报

千问 | 2009-6-26 11:09:25 | 显示全部楼层
Private Sub Form_Click()Dim i, j, n, s As Integers = 0For i = 1 To 100 For j = 2 To i - 1 If i Mod j0 Then n = 1End IfNext jIf n = 1 Then s = s + iEnd If Next i Print s;End Sub
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行