Page 1 of 2 12 LastLast
Results 1 to 40 of 74

Thread: Prime Numbers

  1. #1

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Question Prime Numbers

    Is there a simple algorithm which can determine if a number is prime?

    I've looked at several and all are very time consuming to do very large numbers.

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    No; you can't get both fast and simple. Also, a professor of mine is referenced on one of those pages, that was unexpected .
    Last edited by jemidiah; Aug 24th, 2009 at 04:20 PM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    I didn't think so but I thought I would ask.

    I found this interesting:
    Code:
    A) Start with all the natural numbers except '1' which is not a prime.
      2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35...
      ^
    B) The leftmost number is prime. Multiply each number in the list by this prime and discard the products.
      2 3   5   7   9    11    13    15    17    19    21    23    25    27    29    31    33    35...
        ^
    C) The number after the previous prime is also a prime. Multiply each number in the list starting from
       the latest prime by the latest prime and discard the products.
      2 3   5   7        11    13          17    19          23    25          29    31          35...
            ^
    Repeat C) indefinitely. On each repetition a new prime is identified (marked ^) until all the primes in
    the starting list have been found.
    Discard the products?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Prime Numbers

    2 x 2 = 4.... take the 4 out of the list. 2 x 3 = 6, now remove 6 from the list. 2 x 4 = 8, remove 8... repeat ad nauseum.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    I'm curious, how large are the numbers you're using?
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    Quote Originally Posted by jemidiah View Post
    I'm curious, how large are the numbers you're using?
    Up to 10^9 and perhaps beyond in the future. I have something I wrote and it seems to work.

    Code:
    ' If result = 0 then number is composite.
    
    half = Fix(Sqr(number))
    
    For denom = 2 To half
        result = (number / denom) - Fix(number / denom)
        If result = 0 Then
            Exit For
        End If
    Next denom
    I give it a range which it steps through and outputs the results to a text file. I don't used "Mod" as it has a size limit. I think "Sqr" is okay being any number to the half power. i.e. number^0.5.

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    You can Step 2 to cut the computation time about in half (though you need to start at 3 and check 2 separately that way). 10^9 isn't actually very large. That fast algorithm I linked would probably be slower on such a "small" number. To be honest I was envisioning RSA-size primes of 1000+ digits.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    I've been a participant in the GIMPS project since 2005. They're dealing with Mersenne prime numbers millions of digits in length. The largest do date has 12,837,064 digits, in the form of 2^42643801. It was discovered on April 12 of this year. Below is a link to their site.

    http://www.mersenne.org/

  9. #9
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: Prime Numbers

    Quote Originally Posted by storm5510 View Post
    Is there a simple algorithm which can determine if a number is prime?
    If you are attempting to discover new primes then I strongly suggest you narrow the field by just focusing on ODD Numbers with DIGITAL ROOTS of 1,4,7,2,5,8. These numbers will be either primes or products of primes > 3 multiplied by primes > 3.

    All other numbers:

    EVEN numbers with digital roots 1,4,7,2,5,8
    ODD & EVEN numbers with digital roots 3,6,9

    will be multiples of 2 or 3 or multiples of both 2 & 3.
    Last edited by TriLogic; Sep 5th, 2009 at 09:59 AM.

  10. #10

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    Quote Originally Posted by TriLogic View Post
    If you are attempting to discover new primes then I strongly suggest you narrow the field by just focusing on ODD Numbers with DIGITAL ROOTS of 1,4,7,2,5,8. These numbers will be either primes or products of primes > 3 multiplied by primes > 3.

    All other numbers:

    EVEN numbers with digital roots 1,4,7,2,5,8
    ODD & EVEN numbers with digital roots 3,6,9

    will be multiples of 2 or 3 or multiples of both 2 & 3.
    Can you elaborate on this just a little more? I am not sure I understand the phrase "digital root".

    I am checking odd numbers ending with 1, 3, 7, and 9. I am using a loop division to check each number in steps of 2.

  11. #11
    New Member
    Join Date
    Sep 2009
    Posts
    4

    Re: Prime Numbers

    Quote Originally Posted by storm5510 View Post
    Can you elaborate on this just a little more? I am not sure I understand the phrase "digital root".

    I am checking odd numbers ending with 1, 3, 7, and 9. I am using a loop division to check each number in steps of 2.
    The problem with checking numbers with terminating digits of 1,3,7 and 9 is that they will include multiples of 3 such as 9,(15),21, 27, 33, 39,...,just keep adding 6 to these numbers and the pattern repeats.(includes terminating digit 5) as 1,7,3,9,5,1,7,3,9,5,...,None of these numbers in this sequence towards infinity are prime.

    Calculating Digital Roots is a method of summing individual digits of any number to a single digit number between 1 and 9.

    http://en.wikipedia.org/wiki/Digital_root.

    9 = 9
    21 = 2 + 1 = 3
    27 = 2 + 7 = 9
    33 = 3 + 3 = 6
    39 = 3 + 9 = 12 = 1 + 2 = 3

    31 = 3 + 1 = 4
    37 = 3 + 7 = 10 = 1
    43 = 4 + 3 = 7



    Any numbers with Digital Root of 3, 6 or 9 will not be a prime number. So calculating digital roots is useful method of 'rooting out' odd numbers which terminate with 1,3,7,9, that have digital roots of 1,4,7 and 2,5,8 but not 3,6 or 9.

    ODD Numbers with digital roots 1,4,7 are found in sequence 1 + 6 = 7 + 6 = 13 + 6 = 19 + 6 = 25 + 6 = 31You can add 12 to terminating digit 9 to eliminate 5's

    So 6n + 1 = digital root of 1, 4 or 7
    And 6n - 1 = digital root of 2, 5, or 8.

    The results of these calculations will give either Prime Number or a prime > 3 multiplied by a prime/primes > 3. This is significant because all other prime factors for all other numbers are based on 2^n and/or 3^n

    Primes emerge within two streams: One emerging from the number 1 + multiples of 6 towards infinity. And the other stream emerging from the number 5 + multiples of 6 towards infinity.

    The odd numbers to check are within:

    1, 7, 13, 19, (25), 31,...stream containing digital roots 1, 4, or 7

    or

    5, 11, 17, 23, 29, (35), 41....,stream containing digital roots 2, 5 or 8

    Sorry if this sounds too confusing to be of any use
    Last edited by TriLogic; Sep 5th, 2009 at 05:26 PM.

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    How fast do u need it?
    This is just a basic one and it takes less that half a second to list all primes upto 100,000

    vb Code:
    1. Dim primeNumbers As New List(Of Integer)
    2. Dim isPrime As Boolean
    3. primeNumbers.Add(2)
    4. For i As Integer = 3 To 100000 Step 2
    5.     isPrime = True
    6.     For Each n As Integer In primeNumbers
    7.         If i Mod n = 0 Then isPrime = False : Exit For
    8.     Next
    9.     If isPrime Then primeNumbers.Add(i)
    10. Next
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    Quote Originally Posted by TriLogic
    Sorry if this sounds too confusing to be of any use.
    It is. I need to study it more.


    Quote Originally Posted by Pradeep1210
    This is just a basic one and it takes less that half a second to list all primes upto 100,000
    And if one wanted to run this indefinitely? What I mean is, it would take a bit more.

    I appreciate everyone replies. What I am using is below:


    Code:
    Function IsPrimeA(ByVal N As ULong) As Short
    
            Dim C As ULong
    
            IsPrimeA = 0
    
            Half = N ^ 0.5
    
            For C = 3 To Half Step 2
                If N Mod C = 0 Then Exit Function
            Next
    
            IsPrimeA = 1
            Return IsPrimeA
    
    End Function
    Last edited by storm5510; Sep 5th, 2009 at 06:42 PM.

  14. #14
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    The digital root tests are conceptually nice, but they require numbers to be stored in base 10 to be efficient, and are basically just as powerful as dividing a couple of times is, likely over-complicating things (at least for a base 2 computer). The code you listed should work fine, up until the limit of the "mod" function. For obscenely large number different methods are needed, but I don't think you're in that range (in fact, if you were, you'd likely need to be using some form of string arithmetic anyway, and your problem could easily get intractable).

    My "fast" link above is apparently the quickest*, provably correct algorithm for finding primes in existence. It's also a bit of a monster. The one you've done is the simplest, and will work quite well for small primes. It's very, very similar to the "simple" link, the Sieve.


    *quickest for all numbers past some point; that point might be larger than the number of atoms in the universe, so you better be using *very* large numbers to bother.
    Last edited by jemidiah; Sep 5th, 2009 at 09:41 PM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  15. #15

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    I suspect I will hit a wall with Mod at 2^32 (4,294,967,296). If I do, I have a work-around I found in the local documentation:

    Code:
    a - (b * Fix(a / b))

    Did you notice my usage of a "half-power"? I can only assume that square root is gone. All I know is that "Sqr" didn't have any meaning. I tried some variations without success.

  16. #16
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    Using ^0.5 one time when you're looping millions (or more) of times is no biggee. I dunno what they did with the function, I still use VB6.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    This is interesting . I compared a few methods and this is what I have:
    vb.net Code:
    1. Private Const MAX As ULong = 10000000
    2. Private Const OutputFormat As String = "{0}. {1} Prime numbers Found in {2} ms"
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.     Dim primeNumbers As New List(Of ULong)
    6.     Dim sw As New Stopwatch
    7.  
    8.     Debug.Print("Normal Method:")
    9.     For pass As Integer = 1 To 5
    10.         sw.Reset()
    11.         sw.Start()
    12.         primeNumbers = NormalMethod()
    13.         sw.Stop()
    14.         Debug.Print(OutputFormat, pass, primeNumbers.Count, sw.ElapsedMilliseconds)
    15.         Application.DoEvents()
    16.     Next
    17.  
    18.     Debug.Print("Sieve Of Eratosthenes:")
    19.     For pass As Integer = 1 To 5
    20.         sw.Reset()
    21.         sw.Start()
    22.         primeNumbers = SieveOfEratosthenes()
    23.         sw.Stop()
    24.         Debug.Print(OutputFormat, pass, primeNumbers.Count, sw.ElapsedMilliseconds)
    25.         Application.DoEvents()
    26.     Next
    27.  
    28.     Debug.Print("Euler's Sieve:")
    29.     For pass As Integer = 1 To 5
    30.         sw.Reset()
    31.         sw.Start()
    32.         primeNumbers = EulersSieve()
    33.         sw.Stop()
    34.         Debug.Print(OutputFormat, pass, primeNumbers.Count, sw.ElapsedMilliseconds)
    35.         Application.DoEvents()
    36.     Next
    37. End Sub
    38.  
    39. Private Function NormalMethod() As List(Of ULong)
    40.     Dim primeNumbers As New List(Of ULong)
    41.     Dim isPrime As Boolean
    42.  
    43.     For i As ULong = 3 To MAX Step 2
    44.         isPrime = True
    45.         For j As Long = 0 To primeNumbers.Count ^ 0.5 - 1
    46.             If i Mod primeNumbers(j) = 0 Then isPrime = False : Exit For
    47.         Next
    48.         If isPrime Then primeNumbers.Add(i)
    49.     Next
    50.     primeNumbers.Insert(0, 2)
    51.     Return primeNumbers
    52. End Function
    53.  
    54. Private Function SieveOfEratosthenes() As List(Of ULong)
    55.     Dim primeNumbers As New List(Of ULong)
    56.     Dim numbers(MAX) As Integer '1 = prime, -1 = not prime
    57.  
    58.     For i As ULong = 2 To MAX
    59.         If numbers(i) = 0 Then
    60.             numbers(i) = 1
    61.             primeNumbers.Add(i)
    62.             For j As ULong = i To MAX Step i
    63.                 numbers(j) = -1
    64.             Next
    65.         End If
    66.     Next
    67.     Return primeNumbers
    68. End Function
    69.  
    70. Private Function EulersSieve() As List(Of ULong)
    71.     Dim primeNumbers As New List(Of ULong)
    72.     Dim numbers(MAX) As Integer '1 = prime, -1 = not prime
    73.  
    74.     For i As ULong = 2 To MAX
    75.         If numbers(i) = 0 Then
    76.             numbers(i) = 1
    77.             primeNumbers.Add(i)
    78.             For j As ULong = i To MAX
    79.                 Dim pos As ULong = j * i
    80.                 If pos > MAX Then Exit For
    81.                 numbers(pos) = -1
    82.             Next
    83.         End If
    84.     Next
    85.     Return primeNumbers
    86. End Function

    Result:
    Code:
    Normal Method:
    1. 664579 Prime numbers Found in 21563 ms
    2. 664579 Prime numbers Found in 21550 ms
    3. 664579 Prime numbers Found in 21547 ms
    4. 664579 Prime numbers Found in 21540 ms
    5. 664579 Prime numbers Found in 21561 ms
    Sieve Of Eratosthenes:
    1. 664579 Prime numbers Found in 952 ms
    2. 664579 Prime numbers Found in 1190 ms
    3. 664579 Prime numbers Found in 2410 ms
    4. 664579 Prime numbers Found in 2540 ms
    5. 664579 Prime numbers Found in 2468 ms
    Euler's Sieve:
    1. 664579 Prime numbers Found in 1639 ms
    2. 664579 Prime numbers Found in 1627 ms
    3. 664579 Prime numbers Found in 1633 ms
    4. 664579 Prime numbers Found in 1630 ms
    5. 664579 Prime numbers Found in 1668 ms
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    Yes, very interesting. Thank you.

    I am not sure how I would implement this as it uses a list. My function receives one number at a time with no limit as to how large the number is.

    The Mod function is past 2^32 now. No problems. So, I'll just stick with it and see how far it can go.

    Below is another variation I came across:

    Code:
    i = Sqr(M)
            For j = 0 To i Step 60
                If M Mod (j + 11) = 0 Then Exit Function
                If M Mod (j + 13) = 0 Then Exit Function
                If M Mod (j + 17) = 0 Then Exit Function
                If M Mod (j + 19) = 0 Then Exit Function
                If M Mod (j + 23) = 0 Then Exit Function
                If M Mod (j + 29) = 0 Then Exit Function
                If M Mod (j + 31) = 0 Then Exit Function
                If M Mod (j + 37) = 0 Then Exit Function
                If M Mod (j + 41) = 0 Then Exit Function
                If M Mod (j + 43) = 0 Then Exit Function
                If M Mod (j + 47) = 0 Then Exit Function
                If M Mod (j + 53) = 0 Then Exit Function
                If M Mod (j + 59) = 0 Then Exit Function
                If M Mod (j + 61) = 0 Then Exit Function
                If M Mod (j + 67) = 0 Then Exit Function
                DoEvents
            Next j
    Last edited by storm5510; Sep 6th, 2009 at 01:19 PM.

  19. #19
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    ok.. so let's modify our functions to test whether the given number is prime number or not.

    vb.net Code:
    1. Private Const OutputFormat2 As String = "{0}. Result = {1}      Time Taken = {2} ms"
    2.  
    3. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    4.     Dim IsPrime As Boolean
    5.     Dim sw As New Stopwatch
    6.  
    7.     Dim number As ULong = ULong.Parse(TextBox1.Text)
    8.  
    9.     Debug.Print("Testing whether " & TextBox1.Text & " is Prime Number or not ...")
    10.  
    11.     Debug.Print("Normal Method:")
    12.     For pass As Integer = 1 To 5
    13.         sw.Reset()
    14.         sw.Start()
    15.         IsPrime = IsPrimeByNormalMethod(number)
    16.         sw.Stop()
    17.         Debug.Print(OutputFormat2, pass, IsPrime, sw.ElapsedMilliseconds)
    18.         Application.DoEvents()
    19.     Next
    20.  
    21.     Debug.Print("Sieve Of Eratosthenes:")
    22.     For pass As Integer = 1 To 5
    23.         sw.Reset()
    24.         sw.Start()
    25.         IsPrime = IsPrimeBySieveOfEratosthenes(number)
    26.         sw.Stop()
    27.         Debug.Print(OutputFormat2, pass, IsPrime, sw.ElapsedMilliseconds)
    28.         Application.DoEvents()
    29.     Next
    30.  
    31.     Debug.Print("Euler's Sieve:")
    32.     For pass As Integer = 1 To 5
    33.         sw.Reset()
    34.         sw.Start()
    35.         IsPrime = IsPrimeByEulersSieve(number)
    36.         sw.Stop()
    37.         Debug.Print(OutputFormat2, pass, IsPrime, sw.ElapsedMilliseconds)
    38.         Application.DoEvents()
    39.     Next
    40. End Sub
    41.  
    42. Private Function IsPrimeByNormalMethod(ByVal number As ULong) As Boolean
    43.     Dim primeNumbers As New List(Of ULong)
    44.     Dim isPrime As Boolean
    45.  
    46.     For i As ULong = 3 To number Step 2
    47.         isPrime = True
    48.         For j As Long = 0 To primeNumbers.Count ^ 0.5 - 1
    49.             If i Mod primeNumbers(j) = 0 Then isPrime = False : Exit For
    50.         Next
    51.         If isPrime Then primeNumbers.Add(i)
    52.     Next
    53.     primeNumbers.Insert(0, 2)
    54.     Return primeNumbers.Contains(number)
    55. End Function
    56.  
    57. Private Function IsPrimeBySieveOfEratosthenes(ByVal number As ULong) As Boolean
    58.     Dim numbers(number) As Integer '1 = prime, -1 = not prime
    59.  
    60.     For i As ULong = 2 To number
    61.         If numbers(i) = 0 Then
    62.             numbers(i) = 1
    63.             For j As ULong = i * 2 To number Step i
    64.                 numbers(j) = -1
    65.             Next
    66.             If numbers(number) <> 0 Then Exit For
    67.         End If
    68.     Next
    69.     Return numbers(number) = 1
    70. End Function
    71.  
    72. Private Function IsPrimeByEulersSieve(ByVal number As ULong) As Boolean
    73.     Dim numbers(number) As Integer '1 = prime, -1 = not prime
    74.  
    75.     For i As ULong = 2 To number
    76.         If numbers(i) = 0 Then
    77.             numbers(i) = 1
    78.             For j As ULong = i To number
    79.                 Dim pos As ULong = j * i
    80.                 If pos > number Then Exit For
    81.                 numbers(pos) = -1
    82.             Next
    83.             If numbers(number) <> 0 Then Exit For
    84.         End If
    85.     Next
    86.     Return numbers(number) = 1
    87. End Function

    Testing against 16769023, this is what I get on my system:
    Code:
    Testing whether 16769023 is Prime Number or not ...
    Normal Method:
    1. Result = True      Time Taken = 45153 ms
    2. Result = True      Time Taken = 46126 ms
    3. Result = True      Time Taken = 46061 ms
    4. Result = True      Time Taken = 46116 ms
    5. Result = True      Time Taken = 45119 ms
    Sieve Of Eratosthenes:
    1. Result = True      Time Taken = 1801 ms
    2. Result = True      Time Taken = 4053 ms
    3. Result = True      Time Taken = 4183 ms
    4. Result = True      Time Taken = 4225 ms
    5. Result = True      Time Taken = 4184 ms
    Euler's Sieve:
    1. Result = True      Time Taken = 2718 ms
    2. Result = True      Time Taken = 2863 ms
    3. Result = True      Time Taken = 2781 ms
    4. Result = True      Time Taken = 2774 ms
    5. Result = True      Time Taken = 2797 ms
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  20. #20
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    aah.. it seems like this test is flawed..
    1. We don't need to loop thru and store the prime numbers in IsPrimeByNormalMethod() function, which would bring down the time taken.
    2. I can't get past the INTEGER limit for number of items in array. So the IsPrimeBySieveOfEratosthenes() and IsPrimeByEulersSieve() don't really work for LONG datatypes.

    Please ignore the above post.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  21. #21

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    There is something I don't get here:

    Line 73 shows a dimension statement for an array. Why an array for only one number at a time?

  22. #22
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    Quote Originally Posted by storm5510 View Post
    There is something I don't get here:

    Line 73 shows a dimension statement for an array. Why an array for only one number at a time?
    How else would we keep a track of which numbers we have crossed out as not primes?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  23. #23
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    Quote Originally Posted by storm5510 View Post
    It is. I need to study it more.




    And if one wanted to run this indefinitely? What I mean is, it would take a bit more.

    I appreciate everyone replies. What I am using is below:


    Code:
    Function IsPrimeA(ByVal N As ULong) As Short
    
            Dim C As ULong
    
            IsPrimeA = 0
    
            Half = N ^ 0.5
    
            For C = 3 To Half Step 2
                If N Mod C = 0 Then Exit Function
            Next
    
            IsPrimeA = 1
            Return IsPrimeA
    
    End Function
    Interestingly this function categorizes 18446744073709551608 as a prime number.. though I don't exactly know why. Clearly this number is not prime as it ends in 8 and is thus divisible by 2.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  24. #24
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    okk... I get it now.
    You missed testing it with 2

    So it should be something like this:
    vb.net Code:
    1. Private Function IsPrimeA(ByVal number As ULong) As Boolean
    2.     If number Mod 2 = 0 Then Return False
    3.     For C As ULong = 3 To number ^ 0.5 Step 2
    4.         If number Mod C = 0 Then Return False
    5.     Next
    6.     Return True
    7. End Function

    And the biggest prime number we would be able to test with it is 18446744073709551557.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  25. #25
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Prime Numbers

    Sieve of Eratosthenes

    This might help

  26. #26
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    I feel silly that I didn't catch that divisible by 2 case (especially when I had mentioned it earlier, "...though you need to start at 3 and check 2 separately that way..."). Whoops.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  27. #27
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    Have a look at this class
    http://www.codeproject.com/KB/cs/biginteger.aspx

    This seems to have a capacity more than ULong and also has a method to determine whether a number is prime or not. Though I've not used it or tested its performance.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  28. #28

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Question Re: Prime Numbers

    http://go.internet.com/?id=474X1146&...iginteger.aspx

    Server Not Found
    .
    .

    2^49836383

    How does anyone represent something like this in a computer and work with it? This is 15,002,247 digits long.

  29. #29
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Prime Numbers

    Jemediah, Techgnome, et al. OP said he only needed primes up to 10^10 in size. Why not use Logophobic's prime number generator (or mine which is a bit slower) to first generate and store all of these primes in a file. Then search the appropriate file to see if a match exists.

    If it does not, then that solves the problem. Seem reasonable?
    Doctor Ed

  30. #30

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    Quote Originally Posted by Code Doc View Post
    Jemediah, Techgnome, et al. OP said he only needed primes up to 10^10 in size...
    I had to quote this just to make sure I was seeing it properly.

    10^10 is 10,000,000,000. Wow!

    I have everything up to 4,346,078,071. I have 2,299 files of 1 MB each. They are packed into RAR archives of 100 files each. Average archive size is 20 MB, so the pack very well. There is also an index.

  31. #31
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Prime Numbers

    I wonder. Could you store these as 4-byte long integers that could reach 2,147,483,648 in size? You pick up 1 byte for every prime larger than 9,999, 2 bytes for every prime larger than 99,999, etc. Not sure if that helps any, but the compression improves as the primes get bigger and there are more of them at the top than at the bottom. The first 200 million primes would thus require 800 Mb.

    How large is the 200 millionth prime number?

    After you get above 2^31, you could then go to a file using 8-byte currency to store numbers up to 16-digit accuracy to the left.
    Last edited by Code Doc; Sep 8th, 2009 at 09:00 AM.
    Doctor Ed

  32. #32

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    I'm writing them as plain text using StreamWriter and WriteLine.

    As the numbers get larger, the time to create one file grows. At present, it takes 90 seconds. I guess I should not complain. It could be a lot worse. I have an older variant I did with VB6. That one was slow.

    I run this on a Core 2 Duo. Despite what I've read, it appears to use both cores when running.

    =====

    Edit: It seems to me there should be some way to do this with logarithms.
    Last edited by storm5510; Sep 8th, 2009 at 12:45 PM.

  33. #33
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Prime Numbers

    Quote Originally Posted by storm5510 View Post
    I'm writing them as plain text using StreamWriter and WriteLine.

    As the numbers get larger, the time to create one file grows. At present, it takes 90 seconds. I guess I should not complain. It could be a lot worse. I have an older variant I did with VB6. That one was slow.

    I run this on a Core 2 Duo. Despite what I've read, it appears to use both cores when running.
    =====
    Edit: It seems to me there should be some way to do this with logarithms.
    I think you may have forgotten to use Logophobic's code to generate the prime numbers. I have seen nothing faster than his program, and I have given up writing code that can beat it.

    Second, consider my recommendation. Store all prime numbers on disk that are less than 2^31 as long integers. Then store the larger ones and all future ones on subsequent files as currency that requires 8 bytes per prime. Your program can continuously check the value of the prime and store the number in the appropriate files as needed. That's a small sacrifice.

    Eventually, the mass storage capacity of your computer will be exceeded, but that may take hours to accomplish. Meanwhile, you can display the progress of your program using standard VB6 controls.
    Doctor Ed

  34. #34

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    Quote Originally Posted by Code Doc View Post
    I think you may have forgotten to use Logophobic's code to generate the prime numbers.
    Logophobic?

    Where can I find this?

  35. #35
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Prime Numbers

    I don't believe logarithms would help store prime numbers more compactly. The compression ratio should be wonderful, though, if you store primes in a file--but then, you're sacrificing speed while you uncompress the file. How quickly do you need to test primality?

    There are a wide number of fast probabilistic tests. A simple one is the Fermat Primality Test. The modular exponentiation can be done quickly; the number of exceptions (if you run the full test) is relatively small--there are only 32 in the numbers from 1 to 500,000.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  36. #36
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Prime Numbers

    Quote Originally Posted by techgnome View Post
    2 x 2 = 4.... take the 4 out of the list. 2 x 3 = 6, now remove 6 from the list. 2 x 4 = 8, remove 8... repeat ad nauseum.

    -tg
    Wouldn't an easier way be to remove any number that "2" can go into?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  37. #37
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Prime Numbers

    Quote Originally Posted by Nightwalker83 View Post
    Wouldn't an easier way be to remove any number that "2" can go into?
    SieveOfEratosthenes and EulersSieve in post #17 There is just a small difference between the two, but that has a considerable difference on performance.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  38. #38
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Prime Numbers

    Quote Originally Posted by storm5510 View Post
    Logophobic?

    Where can I find this?
    Here's a good thread to study:
    http://www.vbforums.com/showthread.p...t=Prime+Number

    Note my suggestions. Also Merri shows a link to his code, which is usually spotless and certainly worth examining. Note that my code may be slower but does not require the memory array of primes that Logophobic's code does. That will be a problem when your list of stored primes becomes huge.
    Doctor Ed

  39. #39

    Thread Starter
    Hyperactive Member storm5510's Avatar
    Join Date
    Jul 2009
    Location
    Indiana, U.S.A.
    Posts
    329

    Re: Prime Numbers

    This looks good, but with on caveat. It uses a list.

    Code:
    Dim MyNumber As Long
      Dim Root As Long
      Dim Prime(19999) As Long
      Dim Count As Long
      Dim I As Long
      
      MyNumber = 2
      Prime(0) = 2
      Count = 1
      Do While Count < 20000
        MyNumber = MyNumber + 1
        Root = Int(Sqr(MyNumber))
        I = 0
        Do Until Prime(I) > Root
          If MyNumber Mod Prime(I) = 0 Then Exit Do
          I = I + 1
        Loop
        If Prime(I) > Root Then
          Prime(Count) = MyNumber
          Count = Count + 1
        End If
      Loop
      
      List1.Clear
      For I = 0 To 19999
        List1.AddItem CStr(Prime(I))
      Next I
    I need to be able to run without using a list. Each value I find is sent to a data file. There is no ceiling on the number of values. I designed my program to be able to run for years, literally.
    Last edited by storm5510; Sep 9th, 2009 at 03:05 PM.

  40. #40
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Prime Numbers

    It's easy to send the prime to a data file once it is found, and I recommend a random access file. However, Logophobic's code is limited by the array size-- Prime(19999). So, 20,000 numbers is about it for a list box. You can likely bump that to several million and store them on disk, but I thought your objective was to get billions of them. So, the array of primes is out.

    Take a look at my slower but simple code that does not require an array. It would be easy to add the prime to a random access file and store the first 2^31 primes as 4-byte long integers. Then when that limit is reached, close that file and open up another file and store the rest of the primes as an 8-byte currency variable. The code below is limited to the long integer.

    Code:
    Dim MyNumber As Long, NumFactors As Integer, PrimeCount as Long
    
    Private Sub Form_Load()
    MyNumber = 1
    Do 
        NumFactors = 0
        MyNumber = MyNumber + 1
        For I = 1 To Sqr(MyNumber)
            If MyNumber / I = MyNumber \ I Then
                NumFactors = NumFactors + 1
                If NumFactors > 1 Then Exit For ' Not a prime
            End If
        Next
        If NumFactors = 1 Then ' Prime Found
            PrimeCount = PrimeCount + 1
            ' Store prime on disk using code here
    
        End IF
    Loop
    End Sub
    Doctor Ed

Page 1 of 2 12 LastLast

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