Dim PrimeTest As Double
Dim PrimeDiv As Double
Dim DivValue As Variant
Dim flag As Integer
Sub CalcPrime()
PrimeTest = Range("A2")
PrimeDiv = 0
flag = 0
Do
PrimeTest = PrimeTest + 1
PrimeDivision:
flag = 0
PrimeDiv = 1
While PrimeDiv <= (PrimeTest) / 2
DivValue = PrimeTest / PrimeDiv
If DivValue = 1 Then
flag = flag + 1
ElseIf DivValue = Int(DivValue) Then
flag = flag + 1
ElseIf DivValue = PrimeTest Then
flag = flag + 1
End If
PrimeDiv = PrimeDiv + 1
Wend
If flag = 1 Then GoTo PrimeConfirm
If flag <> 1 Then PrimeTest = PrimeTest + 1
GoTo PrimeDivision
PrimeConfirm:
Range("A2") = PrimeTest
flag = 0
Loop Until 1 > 1 + 1
End Sub
Every now and then, I like to come back to my code from the past, to see if I can improve it. I think I've found a way, but I'd like to run it past you guys first...
This code tests every number halfway up to the number being checked, to see if it's a factor (the reason being that after halfway, there are no factors of any number)
Now, the question is, is every non-prime number divisible by 2 or 3? (1 is irrelevant IMO)
If so, I could make an prime number array, with the first two numbers being 2 and 3 (themselves prime numbers), and so test a number by every non-zero prime in this array?
That would probably speed up the numbers by a fair bit... seeing as I'm got it up to 80,021, from 1
The other thing I thought of doing, which I know would work, is to cause it to loop back to PrimeDivision after a factor is found...
Also, note the "cute trick" that I used on the "loop until 1 > 1+1"... I can't remember who picked me up on the "loop root 3" in my scanf thread, but hey, I told you I liked shortcuts
Thanks guys,
IntelMole