Hello, I salvaged the following piece of code from:
http://www.vbforums.com/showpost.php...65&postcount=8
This code works fine, but the problem comes when I need to get all the primes above 100 million. I am familar (at least a little bit) with the BigInteger class in .NET 4.0, but when I replace all the instances of "Integer" with "BigInteger" in the following code I get this error:
Value of type 'System.Collections.Generic.List(Of System.Numerics.BigInteger)' cannot be converted to 'System.Collections.Generic.List(Of Integer)'.
(The reason I need primes above 100 million is that I'm trying to work through some projects on www.projecteuler.net)
Anyone have some ideas on how to modify the code to work with BigIntegers, Thank you very much for your help. If you need more code examples please ask.
vb.net Code:
'Sieve of Eratosthenes 'http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes '1. Create a contiguous list of numbers from two to some highest number n. '2. Strike out from the list all multiples of two (4, 6, 8 etc.). '3. The list's next number that has not been struck out is a prime number. '4. Strike out from the list all multiples of the number you identified in the previous step. '5. Repeat steps 3 and 4 until you reach a number that is greater than the square root of n (the highest number in the list). '6. All the remaining numbers in the list are prime. Private Function Sieve_of_Eratosthenes(ByVal MaxNum As Integer) As List(Of Integer) 'tested to MaxNum = 10,000,000 - on 1.8Ghz Laptop it took 1.4 seconds Dim thePrimes As New List(Of Integer) Dim toNum As Integer = MaxNum, stpw As New Stopwatch If toNum > 1 Then 'the first prime is 2 stpw.Start() thePrimes.Capacity = toNum 'size the list Dim idx As Integer Dim stopAT As Integer = CInt(Math.Sqrt(toNum) + 1) '1. Create a contiguous list of numbers from two to some highest number n. '2. Strike out from the list all multiples of 2, 3, 5. For idx = 0 To toNum If idx > 5 Then If idx Mod 2 <> 0 _ AndAlso idx Mod 3 <> 0 _ AndAlso idx Mod 5 <> 0 Then thePrimes.Add(idx) Else thePrimes.Add(-1) Else thePrimes.Add(idx) End If Next 'mark 0,1 and 4 as non-prime thePrimes(0) = -1 thePrimes(1) = -1 thePrimes(4) = -1 Dim aPrime, startAT As Integer idx = 7 'starting at 7 check for primes and multiples Do '3. The list's next number that has not been struck out is a prime number. '4. Strike out from the list all multiples of the number you identified in the previous step. '5. Repeat steps 3 and 4 until you reach a number that is greater than the square root of n (the highest number in the list). If thePrimes(idx) <> -1 Then ' if equal to -1 the number is not a prime 'not equal to -1 the number is a prime aPrime = thePrimes(idx) 'get rid of multiples startAT = aPrime * aPrime For mltpl As Integer = startAT To thePrimes.Count - 1 Step aPrime If thePrimes(mltpl) <> -1 Then thePrimes(mltpl) = -1 Next End If idx += 2 'increment index Loop While idx < stopAT '6. All the remaining numbers in the list are prime. thePrimes = thePrimes.FindAll(Function(i As Integer) i <> -1) stpw.Stop() Debug.WriteLine(stpw.ElapsedMilliseconds) End If Return thePrimes End Function
And here is what I changed it to:
vb.net Code:
Private Function Sieve_of_Eratosthenes(ByVal MaxNum As BigInteger) As List(Of BigInteger) 'tested to MaxNum = 10,000,000 - on 1.8Ghz Laptop it took 1.4 seconds Dim thePrimes As New List(Of BigInteger) Dim toNum As BigInteger = MaxNum, stpw As New Stopwatch If toNum > 1 Then 'the first prime is 2 stpw.Start() thePrimes.Capacity = toNum 'size the list Dim idx As BigInteger Dim stopAT As BigInteger = Math.Sqrt(toNum) + 1 '1. Create a contiguous list of numbers from two to some highest number n. '2. Strike out from the list all multiples of 2, 3, 5. For idx = 0 To toNum If idx > 5 Then If idx Mod 2 <> 0 _ AndAlso idx Mod 3 <> 0 _ AndAlso idx Mod 5 <> 0 Then thePrimes.Add(idx) Else thePrimes.Add(-1) Else thePrimes.Add(idx) End If Next 'mark 0,1 and 4 as non-prime thePrimes(0) = -1 thePrimes(1) = -1 thePrimes(4) = -1 Dim aPrime, startAT As BigInteger idx = 7 'starting at 7 check for primes and multiples Do '3. The list's next number that has not been struck out is a prime number. '4. Strike out from the list all multiples of the number you identified in the previous step. '5. Repeat steps 3 and 4 until you reach a number that is greater than the square root of n (the highest number in the list). If thePrimes(idx) <> -1 Then ' if equal to -1 the number is not a prime 'not equal to -1 the number is a prime aPrime = thePrimes(idx) 'get rid of multiples startAT = aPrime * aPrime For mltpl As BigInteger = startAT To thePrimes.Count - 1 Step aPrime If thePrimes(mltpl) <> -1 Then thePrimes(mltpl) = -1 Next End If idx += 2 'increment index Loop While idx < stopAT '6. All the remaining numbers in the list are prime. thePrimes = thePrimes.FindAll(Function(i As BigInteger) i <> -1) stpw.Stop() Console.WriteLine(stpw.ElapsedMilliseconds & " milliseconds to generate primes") End If Return thePrimes End Function




Reply With Quote