Prime pattern 6 +/- 1
#8
I sat and wrote one of these this morning, just to pass the time:

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.  Wink
Reply


Messages In This Thread
Prime pattern 6 +/- 1 - by SMcNeill - 06-30-2022, 10:49 AM
RE: Prime pattern 6 +/- 1 - by bplus - 06-30-2022, 12:25 PM
RE: Prime pattern 6 +/- 1 - by bplus - 06-30-2022, 12:29 PM
RE: Prime pattern 6 +/- 1 - by david_uwi - 06-30-2022, 12:46 PM
RE: Prime pattern 6 +/- 1 - by bplus - 06-30-2022, 01:32 PM
RE: Prime pattern 6 +/- 1 - by david_uwi - 06-30-2022, 02:39 PM
RE: Prime pattern 6 +/- 1 - by triggered - 07-01-2022, 11:29 AM
RE: Prime pattern 6 +/- 1 - by SMcNeill - 07-01-2022, 11:40 AM
RE: Prime pattern 6 +/- 1 - by bplus - 07-01-2022, 12:51 PM
RE: Prime pattern 6 +/- 1 - by bplus - 07-01-2022, 01:06 PM
RE: Prime pattern 6 +/- 1 - by bplus - 07-01-2022, 01:15 PM
RE: Prime pattern 6 +/- 1 - by david_uwi - 07-01-2022, 03:55 PM
RE: Prime pattern 6 +/- 1 - by triggered - 07-07-2022, 10:46 AM



Users browsing this thread: 6 Guest(s)