Results 1 to 9 of 9

Thread: Prime Numbers

  1. #1
    appi101
    Guest

    Prime Numbers

    Hi

    What would be the fastest way of finding primes numbers. I was able to find 10 million of them in 4762 secs on a p3-866 on win98.

    Appi

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    What is the code you are currently using? Then we can see if it can be optimised.

  3. #3
    appi101
    Guest
    Hi

    THanks for the reply. Here's the source code.

    Appi

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's pretty much the fastest way to retrieve the primes, in two ways, 1. You check if it is divisble with earlier found primes effectively eliminating checks with numbers that can be factorized with earlierly checked primes. 2. No need to go beyond the squareroot of the value to check since the factors always comes in pairs where the highest possible pair can not exceed the root. So the algoritm is very optimized.
    Anyway implementing it vb slows it down, here's what I did to remove the bottlenecks:
    1. Replaced all double precision floating points except the timer with long integers. replaced t string with long, yes that inexcusable misstake.
    2. Inlined the IsPrime function, the stack processing in vb is heavy. The extra Do-Loop is just there to function as a exit point.
    3. Narrowed the UI processing by 1000, the refreshing of the label as well as processing messages with doevents is quite a hog.

    Code:
    Dim Total As Long
    Dim TotalAsYet As Long
    Dim Primes() As Long
    Option Explicit
    
    Private Sub Command1_Click()
    Dim Tmr As Double, i&, ff%, t&, t2$, x&, root&, IsPrime As Boolean
    Tmr = Timer
    t2 = InputBox("How Many Primes do you want")
    If t2 = "" Then Exit Sub
    t = Val(t2)
    ReDim Preserve Primes(1 To t)
    Primes(1) = 2
    i = 3
    TotalAsYet = 1
    Label1.Caption = "Calculating"
    ReDim Preserve Primes(1 To t)
    Do
        Do
            root = Sqr(i)
            For x = 1 To TotalAsYet
                If Primes(x) <= root Then
                    If i Mod Primes(x) = 0 Then
                        IsPrime = False
                        Exit Do
                    End If
                Else
                    IsPrime = True
                    Exit Do
                End If
            Next
            IsPrime = True
        Loop While False
        
        If IsPrime Then
            TotalAsYet = TotalAsYet + 1
            Primes(TotalAsYet) = i
            If i Mod 1000 = 1 Then
                lblprog.Caption = TotalAsYet / t * 100
                DoEvents
            End If
        End If
        i = i + 2
    Loop While (TotalAsYet <> t)
    lblprog.Caption = "Time Taken " & Timer - Tmr
    ff = FreeFile
    Open "c:\primes.txt" For Output As ff
    Label1.Caption = "Writing"
        For i = 1 To TotalAsYet
            Print #ff, Str(Primes(i))
            DoEvents
        Next i
    Close #ff
    Label1.Caption = "Finished"
    
    End Sub
    17 secs on a AMD7-600 on win98 for 100000 primes
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I forgot to compile. Here's results from the compiled apps with 100000 primes:
    39.9 secs with the bottlenecks
    5.2 secs without
    762% more efficient

    As said, the algoritm was very optimized, it's just the implementation that was bad.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Finally got your code (was away from pc yesterday pm). I haven't timed this algorithm or checked to see how scalable it is, but here is an alternative function for you:

    VB Code:
    1. Option Explicit
    2.  
    3. Function PrimeArray(numberOfPrimes As Long) As Long()
    4.     Dim found As Long, n As Long, i As Long, sr As Long
    5.    
    6.     '// we know the size of the result in advance
    7.     ReDim result(1 To numberOfPrimes) As Long
    8.    
    9.     '// "2" is the first prime number
    10.     result(1) = 2: found = 1
    11.     n = 1
    12.    
    13.     Do While found < numberOfPrimes
    14.         '// all other prime numbers are odd, so we can skip even numbers
    15.         n = n + 2
    16.         sr = Sqr(n)
    17.        
    18.         '// let's check if N is a prime number
    19.         For i = 1 To found
    20.            
    21.             If (n Mod result(i)) = 0 Then Exit For
    22.            
    23.             If result(i) >= sr Then
    24.                 'canot have factor > square root, so n must be prime
    25.                 found = found + 1
    26.                 result(found) = n
    27.                 Exit For
    28.             End If
    29.        
    30.         Next
    31.  
    32.     Loop
    33.     PrimeArray = result
    34. End Function

    Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim a, i&
    3. a = PrimeArray(100000)
    4. For i = 1 To UBound(a)
    5.     Debug.Print a(i)
    6. Next
    7.  
    8. End Sub

  7. #7
    appi101
    Guest
    Hi

    What is the meaning of stack processing?

    Thanks a lot for the code.

    Appi

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It is something your app does to allocate/deallocate memory for each of your function variables and parameters, it's all lowlevel stuff so nothing a vb programmer should worry about. It does take about 100 cpu cycles so it can be considered a hog if called really frequently.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    appi101
    Guest
    Hi

    Thanks a lot for the assistance.

    Appi

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