2009年4月13日 星期一

質因數

image

'判斷n是否為質數
'Public Class Form1
'    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'        Dim n = 2
'        Dim c = 0
'        For i As Integer = 1 To n
'            If n Mod i = 0 Then
'                c = c + 1
'            End If
'        Next
'        If c = 2 Then
'            MsgBox(n & "是質數")
'        End If
'    End Sub
'End Class

'練習1:列出1-30的所有質數
'Public Class Form1
'    Dim str1
'    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'        For n As Integer = 1 To 30
'            Dim c = 0
'            For i As Integer = 1 To n
'                If n Mod i = 0 Then
'                    c = c + 1
'                End If
'            Next
'            If c = 2 Then
'                str1 = str1 & n & Space(2)
'            End If
'        Next
'        MsgBox(str1)
'    End Sub
'End Class

'練習2:列出110的所有質因數
Public Class Form1
    Dim str1
    Dim i
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim n = 110
        Dim j
        For i = 1 To n
            If n Mod i = 0 Then
                Dim c = 0
                For j = 1 To i
                    If i Mod j = 0 Then c = c + 1
                Next
                If c = 2 Then str1 = str1 & i & Space(2)
            End If
        Next
        MsgBox(n & " 的質因數為: " & str1)
    End Sub
End Class

沒有留言:

張貼留言