我已经编写了一个算法,我认为使用Eratosthenes的Sieve来计算高达n的素数是正确的.不幸的是,这个程序依赖于非常大的n值(尝试1000万).这是我写的……
Protected Function Eratosthenes(ByVal n As Integer) As String Dim maxValue As Integer = Math.Sqrt(n) Dim values As Generic.List(Of Integer) = New Generic.List(Of Integer) Dim i As Integer ''//create list. For i = 2 To n values.Add(i) Next For i = 2 To maxValue If values.Contains(i) Then Dim k As Integer For k = i + 1 To n If values.Contains(k) Then If (k Mod i) = 0 Then values.Remove(k) End If End If Next End If Next Dim result As String = "" For i = 0 To values.Count - 1 result = result & " " & values(i) Next Return result End Function
我怎么能加速这个算法?我的瓶颈在哪里?
解决方法
从大型列表中删除元素很慢.
为什么不创建一个布尔值的数组,并在知道它是非素数时将值设置为“True”?
当你找到一个新的素数时,你不需要经历所有更高的值,只需要多个值,将数组元素设置为True.
如果您想要返回它们,您可以为目前为止找到的素数保留单独的列表.
这是一个C#实现,它只是随着它打印出来. (在C#中如果我想返回值,我将返回IEnumerable< T>并使用迭代器块.)
using System; public class ShowPrimes { static void Main(string[] args) { ShowPrimes(10000000); } static void ShowPrimes(int max) { bool[] composite = new bool[max+1]; int maxFactor = (int) Math.Sqrt(max); for (int i=2; i <= maxFactor; i++) { if (composite[i]) { continue; } Console.WriteLine("Found {0}",i); // This is probably as quick as only // multiplying by primes. for (int multiples = i * i; multiples <= max; multiples += i) { composite[multiples] = true; } } // Anything left is a prime,but not // worth sieving for (int i = maxFactor + 1; i <= max; i++) { if (composite[i]) { continue; } Console.WriteLine("Found {0}",i); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。