以下代码列出了从1到10的所有素数:
Dim primeN As Integer
Dim primeI As Integer
Dim primeFlag As Boolean
For primeN = 1 To 10
primeFlag = True
For primeI = 2 To primeN / 2
If primeN Mod primeI = 0 Then
primeFlag = False
End If
Next
If primeFlag Then
Console.WriteLine(primeN)
End If
Next
Console.ReadLine()
如何更改它以便它读取用户输入然后检查它是否为素数?
答案 0 :(得分:0)
创建一个返回true或false标志的函数。
这是我的模块,它生成随机数,然后检查素数:
Module PrimeNumber
Sub Main()
Dim numbers As New ArrayList
For i As Integer = 1 To 20
numbers.Add(CInt(Math.Ceiling(Rnd() * 1000000)))
Next
For Each int As Integer In numbers
Console.WriteLine("{0} -> {1}", int, PrimeNumberCheck(int))
Next
End Sub
Public Function PrimeNumberCheck(ByVal number As Integer) As Boolean
Dim primeI As Integer
Dim primeFlag As Boolean
primeFlag = True
For primeI = 2 To number / 2
If number Mod primeI = 0 Then
Return False
End If
Next
Return primeFlag
End Function
End Module
输出:
答案 1 :(得分:0)
你快到了。而不是循环,只需获取用户输入并将其放入该变量中。然后稍微改变你的信息。
代码:
Dim primeN As Integer
Dim primeI As Integer
Dim primeFlag As Boolean
primeN = Int32.Parse(Console.ReadLine())
primeFlag = True
For primeI = 2 To primeN / 2
If primeN Mod primeI = 0 Then
primeFlag = False
End If
Next
If primeFlag Then
Console.WriteLine("Is prime")
Else
Console.WriteLine("Is not prime")
End If
Console.ReadLine()
像其他人所建议的那样,将逻辑放在一个方法中是一个好主意。
答案 2 :(得分:0)
这是我最快的VBA代码,用于检查数字是否为素数。
Sub ISPRIME()
Dim number_to_be_checked As Long
Dim c As Long
Dim b As Long
number_to_be_checked = 2000000000 'input value here
c = Round(Sqr(number_to_be_checked)) + 1
If number_to_be_checked = 1 Then
MsgBox "Not Prime"
Else
For b = 2 To c
If number_to_be_checked Mod b = 0 And c <> b Then
MsgBox "Not Prime. Divisible by " & b
Exit Sub
Else
If b = c Then
MsgBox "Prime"
Exit Sub
End If
End If
Next b
End If
End Sub
答案 3 :(得分:-1)
Sub generateprimenumbersbetween()
starting_number = 99990 'input value here
last_number = 99999 'input value here
primenumbers = ""
For a = starting_number To last_number
For b = 2 To a
If a - b * Int(a / b) = 0 And a <> b Then
Exit For
Else
If a = b Then
primenumbers = primenumbers & " " & a
End If
End If
Next b
Next a
MsgBox primenumbers
End Sub
Function primenumbersbetween(starting_number, last_number)
primenumbers = ""
For a = starting_number To last_number
For b = 2 To a
If a - b * Int(a / b) = 0 And a <> b Then
Exit For
Else
If a = b Then
primenumbers = primenumbers & " " & a
End If
End If
Next b
Next a
primenumbersbetween = primenumbers
End Function
'to check if a number is prime
Sub ISPRIME()
number_to_be_checked = 102 'input value here
For b = 2 To number_to_be_checked
If number_to_be_checked - b * Int(number_to_be_checked / b) = 0 And _
number_to_be_checked <> b Then
MsgBox "Not Prime. Divisible by " & b
Exit Sub
Else
If number_to_be_checked = b Then
MsgBox "Prime"
End If
End If
Next b
End Sub
'to check if a number is prime
Function ISPRIME2(number_to_be_checked)
For b = 2 To number_to_be_checked
If number_to_be_checked - b * Int(number_to_be_checked / b) = 0 And _
number_to_be_checked <> b Then
ISPRIME2 = "Not Prime. Divisible by " & b
Exit Function
Else
If number_to_be_checked = b Then
ISPRIME2 = "Prime"
End If
End If
Next b
End Function