Results 1 to 3 of 3

Thread: Generate Primes over 100 Million

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Location
    Amsterdam, NY
    Posts
    85

    Generate Primes over 100 Million

    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:
    1. 'Sieve of Eratosthenes
    2.     'http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
    3.     '1. Create a contiguous list of numbers from two to some highest number n.
    4.     '2. Strike out from the list all multiples of two (4, 6, 8 etc.).
    5.     '3. The list's next number that has not been struck out is a prime number.
    6.     '4. Strike out from the list all multiples of the number you identified in the previous step.
    7.     '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).
    8.     '6. All the remaining numbers in the list are prime.
    9.     Private Function Sieve_of_Eratosthenes(ByVal MaxNum As Integer) As List(Of Integer)
    10.         'tested to MaxNum = 10,000,000 - on 1.8Ghz Laptop it took 1.4 seconds
    11.         Dim thePrimes As New List(Of Integer)
    12.         Dim toNum As Integer = MaxNum, stpw As New Stopwatch
    13.         If toNum > 1 Then 'the first prime is 2
    14.             stpw.Start()
    15.             thePrimes.Capacity = toNum 'size the list
    16.             Dim idx As Integer
    17.             Dim stopAT As Integer = CInt(Math.Sqrt(toNum) + 1)
    18.             '1. Create a contiguous list of numbers from two to some highest number n.
    19.             '2. Strike out from the list all multiples of 2, 3, 5.
    20.             For idx = 0 To toNum
    21.                 If idx > 5 Then
    22.                     If idx Mod 2 <> 0 _
    23.                     AndAlso idx Mod 3 <> 0 _
    24.                     AndAlso idx Mod 5 <> 0 Then thePrimes.Add(idx) Else thePrimes.Add(-1)
    25.                 Else
    26.                     thePrimes.Add(idx)
    27.                 End If
    28.             Next
    29.             'mark 0,1 and 4 as non-prime
    30.             thePrimes(0) = -1
    31.             thePrimes(1) = -1
    32.             thePrimes(4) = -1
    33.             Dim aPrime, startAT As Integer
    34.             idx = 7 'starting at 7 check for primes and multiples
    35.             Do
    36.                 '3. The list's next number that has not been struck out is a prime number.
    37.                 '4. Strike out from the list all multiples of the number you identified in the previous step.
    38.                 '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).
    39.                 If thePrimes(idx) <> -1 Then ' if equal to -1 the number is not a prime
    40.                     'not equal to -1 the number is a prime
    41.                     aPrime = thePrimes(idx)
    42.                     'get rid of multiples
    43.                     startAT = aPrime * aPrime
    44.                     For mltpl As Integer = startAT To thePrimes.Count - 1 Step aPrime
    45.                         If thePrimes(mltpl) <> -1 Then thePrimes(mltpl) = -1
    46.                     Next
    47.                 End If
    48.                 idx += 2 'increment index
    49.             Loop While idx < stopAT
    50.             '6. All the remaining numbers in the list are prime.
    51.             thePrimes = thePrimes.FindAll(Function(i As Integer) i <> -1)
    52.             stpw.Stop()
    53.             Debug.WriteLine(stpw.ElapsedMilliseconds)
    54.         End If
    55.         Return thePrimes
    56.     End Function


    And here is what I changed it to:

    vb.net Code:
    1. Private Function Sieve_of_Eratosthenes(ByVal MaxNum As BigInteger) As List(Of BigInteger)
    2.           'tested to MaxNum = 10,000,000 - on 1.8Ghz Laptop it took 1.4 seconds
    3.           Dim thePrimes As New List(Of BigInteger)
    4.           Dim toNum As BigInteger = MaxNum, stpw As New Stopwatch
    5.           If toNum > 1 Then 'the first prime is 2
    6.                stpw.Start()
    7.                thePrimes.Capacity = toNum 'size the list
    8.                Dim idx As BigInteger
    9.                Dim stopAT As BigInteger = Math.Sqrt(toNum) + 1
    10.                '1. Create a contiguous list of numbers from two to some highest number n.
    11.                '2. Strike out from the list all multiples of 2, 3, 5.
    12.                For idx = 0 To toNum
    13.                     If idx > 5 Then
    14.                          If idx Mod 2 <> 0 _
    15.                          AndAlso idx Mod 3 <> 0 _
    16.                          AndAlso idx Mod 5 <> 0 Then thePrimes.Add(idx) Else thePrimes.Add(-1)
    17.                     Else
    18.                          thePrimes.Add(idx)
    19.                     End If
    20.                Next
    21.                'mark 0,1 and 4 as non-prime
    22.                thePrimes(0) = -1
    23.                thePrimes(1) = -1
    24.                thePrimes(4) = -1
    25.                Dim aPrime, startAT As BigInteger
    26.                idx = 7 'starting at 7 check for primes and multiples
    27.  
    28.                Do
    29.                     '3. The list's next number that has not been struck out is a prime number.
    30.                     '4. Strike out from the list all multiples of the number you identified in the previous step.
    31.                     '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).
    32.                     If thePrimes(idx) <> -1 Then ' if equal to -1 the number is not a prime
    33.                          'not equal to -1 the number is a prime
    34.                          aPrime = thePrimes(idx)
    35.                          'get rid of multiples
    36.                          startAT = aPrime * aPrime
    37.                          For mltpl As BigInteger = startAT To thePrimes.Count - 1 Step aPrime
    38.                               If thePrimes(mltpl) <> -1 Then thePrimes(mltpl) = -1
    39.                          Next
    40.                     End If
    41.                     idx += 2 'increment index
    42.                Loop While idx < stopAT
    43.  
    44.                '6. All the remaining numbers in the list are prime.
    45.                thePrimes = thePrimes.FindAll(Function(i As BigInteger) i <> -1)
    46.                stpw.Stop()
    47.                Console.WriteLine(stpw.ElapsedMilliseconds & " milliseconds to generate primes")
    48.           End If
    49.           Return thePrimes
    50.      End Function
    Last edited by patplays852; Jun 27th, 2010 at 08:28 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width