vb编程 编写程序 找出给定范围内所有满足以下条件的整数 该整数的平方数的各位数字之和为素数
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/21 20:08:59
vb编程 编写程序 找出给定范围内所有满足以下条件的整数 该整数的平方数的各位数字之和为素数
vb编程 编写程序 找出给定范围内所有满足以下条件的整数 该整数的平方数的各位数字之和为素数
vb编程 编写程序 找出给定范围内所有满足以下条件的整数 该整数的平方数的各位数字之和为素数
Private Sub Command1_Click()
Dim I As Integer, J As Integer
Dim A As Integer, B As Integer
Dim S As String, N As Integer
Dim P As String
A = Val(Text1.Text)
B = Val(Text2.Text)
For I = A To B
P = ""
If Prime(Sum(I ^ 2)) = True Then
S = CStr(I ^ 2)
For J = 1 To Len(S) - 1
P = P & Mid(S, J, 1) & "+"
Next
P = P & Right(S, 1) & "=" & Sum(I ^ 2)
List1.AddItem CStr(I) & "^2=" & I ^ 2 & ":" & P
End If
Next
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
List1.Clear
Text1.SetFocus
End Sub
Function Sum(N As Integer) As Integer '求一个整数各位数之和
Dim I As Integer, S As String
S = CStr(N)
For I = 1 To Len(S)
Sum = Sum + Val(Mid(S, I, 1))
Next
End Function
Function Prime(N As Integer) As Boolean '判断一个整数是否为素数
Dim I As Integer
If N <= 1 Then Prime = False: Exit Function
For I = 2 To N / 2
If N Mod I = 0 Then
Prime = False
Exit Function
End If
Next
If I > N / 2 Then Prime = True
End Function
Private Sub Form_Load()
Form1.Caption = "看不清楚标题,这儿自己改"
End Sub