07-01-2022, 11:40 AM
I sat and wrote one of these this morning, just to pass the time:
Seems a little shorter and simpler than what you guys have posted, but the time runs almost *exactly* the same on my machine as the times which bplus has posted in his examples above -- with the exception being the final 100,000,000 test. For some reason, the times for it seems to be about twice the time as what bplus has listed. Are you certain that 20 second time is correct? If so, then why the heck does this hold so perfectly relatable to the other times, and then suddenly jump off course with the final test??
Anywho, I'll play around with this a bit more later and see if I can't come up with a method which might be faster than this. It's always fun to try and optimize something for speed and performance.
Code: (Select All)
$Checking:Off
DefLng A-Z: Const Limit = 10000000: Dim Shared primes(10000000)
primes(1) = 2: primes(2) = 3: n = 2 'preload the first two primes.
t# = Timer
num = -1
Do
num = num + 6
If CheckPrime(num) Then n = n + 1: primes(n) = num
If CheckPrime(num + 2) Then n = n + 1: primes(n) = num + 2
Loop Until num >= Limit
Print Using "###.### seconds to generate ###,###,###,### primes"; Timer - t#, n
Function CheckPrime (num)
s = Sqr(num)
For i = 1 To 10000000
If primes(i) > s Then Exit For Else If num Mod primes(i) = 0 Then Exit Function
Next
CheckPrime = -1
End Function
Seems a little shorter and simpler than what you guys have posted, but the time runs almost *exactly* the same on my machine as the times which bplus has posted in his examples above -- with the exception being the final 100,000,000 test. For some reason, the times for it seems to be about twice the time as what bplus has listed. Are you certain that 20 second time is correct? If so, then why the heck does this hold so perfectly relatable to the other times, and then suddenly jump off course with the final test??
Anywho, I'll play around with this a bit more later and see if I can't come up with a method which might be faster than this. It's always fun to try and optimize something for speed and performance.