![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Sep 2004
Posts: 7
Rep Power: 0
![]() |
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 FunctionFor 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 indexNext index
If index = bound Then
ReDim Preserve primes(bound + 1)
primes(bound + 1) = x
End If
Next xUnfortunately, 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 indexIt'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! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|