vb.net Visual Basic:如何显示 1 和输入数字之间的质数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29332273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:56:14  来源:igfitidea点击:

Visual Basic: How can i display the prime numbers between 1 and the inputted number

vb.netfunctionprimesbasic

提问by Wes

Hello everyone so i'm trying to find the prime numbers of any input. I want them to be in a listbox and the input in a text box. I would like to use two arguments but i don't know how to. this is the code i have i need dire help. I am not the best at visual basic i just need some guidance. My code isn't working but display a drop down box when i press display.

大家好,所以我试图找到任何输入的素数。我希望它们在列表框中,输入在文本框中。我想使用两个参数,但我不知道如何使用。这是我需要紧急帮助的代码。我不是 Visual Basic 中的佼佼者,我只是需要一些指导。我的代码不起作用,但当我按下 display 时会显示一个下拉框。

Public Class Form1
    Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click

        Dim prim As Integer
        Dim test As Integer
        Dim imPrime As Boolean = False
        prim = CInt(txtNum.Text)
        test = prim

        If prim = 1 Then
            imPrime = False
            MessageBox.Show("Enter a number greater than one please")
        Else
            Do While prim >= 2
                For i As Integer = 2 To prim
                    If prim Mod i = 0 Then
                        imPrime = False
                        Exit For
                    Else
                        imPrime = True
                        lstPrime.Items.Add(prim)
                    End If
                Next
            Loop
        End If
        If imPrime = True Then
            lstPrime.Items.Add(prim)
        End If

    End Sub
End Class

回答by John Kennedy M. Aquino

This is my fastest VBA code to generate prime numbers between two numbers.

这是我在两个数字之间生成素数的最快 VBA 代码。

The generated prime numbers are put in clipboard. You will need to open your ms office word and type Ctrl+V to view all the generated prime numbers.

生成的素数放在剪贴板中。您将需要打开您的 ms office word 并键入 Ctrl+V 以查看所有生成的质数。

Sub generateprimenumbersbetween()
Dim starting_number As Long
Dim last_number As Long
Dim primenumbers As Variant
Dim a As Long
Dim b As Long
Dim c As Long
starting_number = 1 'input value here
last_number = 1000000 'input value here
primenumbers = ""
For a = starting_number To last_number
    c = Round(Sqr(a)) + 1
    For b = 2 To c
        If a = 1 Or (a Mod b = 0 And c <> b) Then
            Exit For
        Else
            If b = c Then
                primenumbers = primenumbers & " " & a
                Exit For
            End If
        End If
    Next b
Next a
Dim answer As DataObject
Set answer = New DataObject
answer.SetText primenumbers
answer.PutInClipboard
End Sub

回答by GojiraDeMonstah

I think the while loop is not working as you intend. You need two loops, the first one counting up to the possible prime, and an inner one counting up to the counter in the outer loop.

我认为 while 循环没有按您的预期工作。您需要两个循环,第一个循环计数到可能的素数,一个内部循环计数到外循环中的计数器。

You can find examples everywhere... here's one implemented in C#, but since your question was specifically about a listbox, I've translated it to VB.

您可以在任何地方找到示例...这里是在 C# 中实现的示例,但是由于您的问题专门针对列表框,我已将其翻译为 VB。

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        calculatePrimes()
    End Sub

    Private Sub calculatePrimes()

        Dim prim As Integer
        Dim count As Integer = 0

        prim = CInt(Me.TextBox1.Text)
        If prim < 3 Then
            MsgBox("Please enter a bigger number")
            Return
        End If

        Me.ListBox1.Items.Clear()

        For i As Integer = 1 To prim
            Dim isPrime As Boolean = True
            For j As Integer = 2 To i
                If (i Mod j <> 0) Then count = count + 1
            Next
            If count = (i - 2) Then Me.ListBox1.Items.Add(i)
            count = 0
        Next

    End Sub
End Class

(This assumes you have a textbox for input called TextBox1 and a listbox for display called ListBox1)

(这假设您有一个名为 TextBox1 的输入文本框和一个名为 ListBox1 的显示列表框)