Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 29th, 2004, 9:24 AM   #1
Johnny_English
Newbie
 
Join Date: Sep 2004
Posts: 7
Rep Power: 0 Johnny_English is on a distinguished road
Public Function primechk(max As Integer)
I'm still writing the prime number algorithm and so far I have this code for my function:

Dim primes() As Integer
ReDim primes(0)
primes(0) = 2
For x = 3 To max
  bound = UBound(primes)
  For index = 0 To bound
    If x Mod primes(index) = 0 Then
      Exit For
    End If
    indices = index
  Next index
  If indices = bound Then
    ReDim Preserve primes(bound + 1)
    primes(bound + 1) = x
  End If
  
Next x
  Print primes(bound)
End Function
It works fine, but I'm still optimizing, before I was using this:

For index = 0 To bound
    If x Mod primes(index) = 0 Then
      Exit For
    End If
    If index = bound Then
       ReDim Preserve primes(bound + 1)
       primes(bound + 1) = x
    End If
Next index
But I realized that checking the index each iteration was very inefficient, so I decided to put the If then statement outside the embeded for loop:

Next index
  If index = bound Then
    ReDim Preserve primes(bound + 1)
    primes(bound + 1) = x
  End If
  
Next x

Unfortunately, the program was not working... After a quick print test with the variable index, I came to the realization that it was destroyed as soon as the loop exited. So I decided to use the sligtly more efficient

For index = 0 To bound
    If x Mod primes(index) = 0 Then
      Exit For
    End If
    indices = index
  Next index

It's deffinetly better than repeating the if then and comparisons. But I still feel that the assignment is taking up too much time. In order to make this program truly optimized I want to reuse index.

How do I keep the variable index from being destroyed after the loop is exited so I can reuse its value later?

Thanks!
Johnny_English is offline   Reply With Quote
Old Sep 29th, 2004, 10:09 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
If ou declare a variable inside a loop it only remains for the scope of the
loop, so i would decalre it at the start of the method or make it a gloabal variabel <--- not a good idea to do that one,.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC