Vb.net 找到最大值的素数
我正在写一个程序,可以找到一个指定极限的素数。我试过:Vb.net 找到最大值的素数,vb.net,algorithm,Vb.net,Algorithm,我正在写一个程序,可以找到一个指定极限的素数。我试过: Sub Main() Console.WriteLine("Enter the maximum") Dim primes As List(Of Integer) = New List(Of Integer) Dim m As Integer = Console.ReadLine() Dim odds As List(Of Integer) = GetOdds(m) For Each i In odds
Sub Main()
Console.WriteLine("Enter the maximum")
Dim primes As List(Of Integer) = New List(Of Integer)
Dim m As Integer = Console.ReadLine()
Dim odds As List(Of Integer) = GetOdds(m)
For Each i In odds
Dim x As List(Of Integer) = GetFactors(i)
Dim con As Boolean = (x(0).ToString().Contains(i) Or x(1).ToString().Contains(i))
If x.Count = 2 Then
primes.Add(i)
' *****
End If
Next
Console.WriteLine("The primes are: " + String.Join(", ", primes))
Console.ReadLine()
End Sub
Function GetOdds(ByVal max As Int32) As List(Of Integer)
Dim g As List(Of Integer) = New List(Of Integer)
For i = 2 To max
If i Mod 2 = 0 Then
Continue For
Else : g.Add(i)
End If
Next
Return g
End Function
Function GetFactors(ByVal x As Integer) As List(Of Integer)
Dim factors As List(Of Integer) = New List(Of Integer)
Dim max As Integer = Math.Sqrt(Convert.ToDouble(x))
For i = 1 To max
If x Mod i = 0 Then
factors.Add(i)
If i <> x / i Then
factors.Add(i / x)
End If
End If
Next
Return factors
End Function
在代码中我用星号标记的区域
但是程序会输出太多的2
我该怎么办?谢谢。您只是在检查奇数是否为素数。2是偶数,这就是程序忽略它的原因。 因此,您应该初始化集合,使其已包含2(它是唯一的偶数素数):
然后添加getLobbits(i)虽然您的代码看起来可以工作,但您会发现,为较大的数字查找素数将花费大量时间 一种改良的埃拉斯托烯筛将很好地解决这一问题 在主循环中,不要列出奇数来进行检查,而是从奇数开始,然后按2步进行检查
Dim primes As List(Of Integer) = New List(Of Integer)
primes.Add(2)
For i = 3 To m Step 2
If IsPrime(i) Then
primes.Add(i)
End If
Next
Dim Sieve As New List(Of Integer)({2, 3, 5, 7, 11, 13})
Function IsPrime(num As Integer) As Boolean
If num = 1 Then Return False
'If number is in the sieve it's prime
If Sieve.Contains(num) Then Return True
'Set limit to the square root of the numnber
Dim Max As Integer = CInt(Math.Sqrt(num))
'Since every num will be odd, there's no need to check if it's divisible by 2
Dim I As Integer = 1
'Check if the number is a multiple of any elements in the sieve
While (I < Sieve.Count AndAlso Sieve(I) <= Max)
If (num Mod Sieve(I) = 0) Then Return False
I += 1
End While
'If the number is too big for the sieve to adequately check, build the sieve bigger,
'and check the number against the new primes.
If Max > Sieve.Last Then
For J As Integer = Sieve.Last + 2 To Max Step 2
Dim good As Boolean = True
For K = 0 To Sieve.Count - 1
If J Mod Sieve(K) = 0 Then
good = False
Exit For
End If
Next
If good Then
Sieve.Add(J)
If num Mod J = 0 Then Return False
End If
Next
End If
Return True
End Function
我不敢相信我没有意识到这一点。你可能想看看一种改良的埃拉托斯烯筛。你可能对此感兴趣:。
Dim odds as List(Of Integer)
odds.Add(2)
Dim primes As List(Of Integer) = New List(Of Integer)
primes.Add(2)
For i = 3 To m Step 2
If IsPrime(i) Then
primes.Add(i)
End If
Next
Dim Sieve As New List(Of Integer)({2, 3, 5, 7, 11, 13})
Function IsPrime(num As Integer) As Boolean
If num = 1 Then Return False
'If number is in the sieve it's prime
If Sieve.Contains(num) Then Return True
'Set limit to the square root of the numnber
Dim Max As Integer = CInt(Math.Sqrt(num))
'Since every num will be odd, there's no need to check if it's divisible by 2
Dim I As Integer = 1
'Check if the number is a multiple of any elements in the sieve
While (I < Sieve.Count AndAlso Sieve(I) <= Max)
If (num Mod Sieve(I) = 0) Then Return False
I += 1
End While
'If the number is too big for the sieve to adequately check, build the sieve bigger,
'and check the number against the new primes.
If Max > Sieve.Last Then
For J As Integer = Sieve.Last + 2 To Max Step 2
Dim good As Boolean = True
For K = 0 To Sieve.Count - 1
If J Mod Sieve(K) = 0 Then
good = False
Exit For
End If
Next
If good Then
Sieve.Add(J)
If num Mod J = 0 Then Return False
End If
Next
End If
Return True
End Function
Function GeneratePrimes(n As Integer) As List(Of Integer)
Dim bits = New BitArray(n + 1, True)
Dim primes = New List(Of Integer)
bits(0) = False
bits(1) = False
For i As Integer = 2 To CInt(Math.Sqrt(n))
If bits(i) Then
For j As Integer = i * i To n Step i
bits(j) = False
Next
primes.Add(i)
End If
Next
For i = CInt(Math.Sqrt(n)) + 1 To n
If bits(i) Then
primes.Add(i)
End If
Next
Return primes
End Function