求100以内的素数,素数是指一个数x除了1和它本身,不能被其他任何整数整除.要求每行输出10个素数.

1个回答

  • Private Sub Command1_Click()

    Dim i,j As Long

    for i=1 to 100

    If prime(i) Then

    Print i;

    j=j+1

    if j mod 10=0 then print

    end if

    next

    End Sub

    Private Function prime(ByVal n As Long) As Boolean '返回值=True是素数,False不是素数

    Dim k As Long

    If n < 2 Then

    prime = False

    Else

    prime = True

    For k = 2 To n - 1

    If (n Mod k) = 0 Then prime = False:Exit For

    Next k

    End If

    End Function