Consulting

Results 1 to 5 of 5

Thread: sieve of eratosthenes vba code explanation

  1. #1

    sieve of eratosthenes vba code explanation

    Hello Guys

    i need some help in understanding the following vba code
    Sub Sieve()
        Const n         As Long = 10 ' numbers to test
        Dim colCand     As Collection   ' candidates
        Dim aiPrim()    As Long         ' primes for output
        Dim iPrim       As Long         ' a prime
        Dim nPrim       As Long         ' running count of primes
        Dim i           As Long         ' scratch index
     
        Set colCand = New Collection
        ReDim aiPrim(1 To n / (Log(n) - 2)) ' high estimate for nPrim
     
        ' initialize candidates
        For i = 2 To n
            colCand.Add Item:=i, Key:=CStr(i)
        Next i
     
        ' we'll be trying to delete numbers in colCand that were already deleted
        On Error Resume Next
     
        Do While colCand.Count
            ' first number in colCand is a prime
            nPrim = nPrim + 1
            iPrim = colCand(1)
            aiPrim(nPrim) = iPrim
            colCand.Remove 1
     
            ' remove all multiples
            For i = 2 To n \ iPrim
                colCand.Remove CStr(i * iPrim)
            Next i
        Loop
     
        'list 'em
        Range("A1").Resize(nPrim).Value = WorksheetFunction.Transpose(aiPrim)
        Beep
    End Sub
    the code is not mine , and allthough there are comments and i know it generate prime numbers , i dont understand the do while loop code , i dont understand what it does in each line , if anyone can read it and explain it in a simple manner .

    and yes i'v tried running the code line by line to figure it out without success.

    thanks in advance for your help guys .

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
     ' initialize candidates
        For i = 2 To n 
            colCand.Add Item:=i, Key:=CStr(i) 
        Next i
    Collects the number of prime number candidates
    Do While colCand.Count 
             ' first number in colCand is a prime
            nPrim = nPrim + 1 
            iPrim = colCand(1) 
            aiPrim(nPrim) = iPrim 
            colCand.Remove 1 
             
             ' remove all multiples
            For i = 2 To n \ iPrim 
                colCand.Remove CStr(i * iPrim) 
            Next i 
        Loop
    DoWhile keeps running the code below for each iteration, the iterations are set by the collection of candidate numbers.
    Loop simply sends it back to DoWhile until colCand count has been exhausted.
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Quote Originally Posted by Simon Lloyd View Post
     ' initialize candidates
        For i = 2 To n 
            colCand.Add Item:=i, Key:=CStr(i) 
        Next i
    Collects the number of prime number candidates
    Do While colCand.Count 
             ' first number in colCand is a prime
            nPrim = nPrim + 1 
            iPrim = colCand(1) 
            aiPrim(nPrim) = iPrim 
            colCand.Remove 1 
             
             ' remove all multiples
            For i = 2 To n \ iPrim 
                colCand.Remove CStr(i * iPrim) 
            Next i 
        Loop
    DoWhile keeps running the code below for each iteration, the iterations are set by the collection of candidate numbers.
    Loop simply sends it back to DoWhile until colCand count has been exhausted.
    i dont know understand the part where the code detect the prime numbers and then seperate them .

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Is this homework or an assignment?
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Do While... While expects a Boolean
    A numerical 0 is a Boolean False, all other numbers are Boolean True

    I like being specific when converting Variable Types...
    Do While CBool(colCand.Count)
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

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