Prime pattern 6 +/- 1 - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26) +---- Forum: bplus (https://qb64phoenix.com/forum/forumdisplay.php?fid=36) +---- Thread: Prime pattern 6 +/- 1 (/showthread.php?tid=577) Pages:
1
2
|
Prime pattern 6 +/- 1 - SMcNeill - 06-30-2022 Here's something I noticed the other day, which I thought might interest you, since you've written a ton of programs regarding prime numbers -- most primes tend to be multiples of 6, +/- 1! 5 is 6 -1 7 is 6 + 1 11 is 6 * 2 -1 13 is 6 * 2 +1 17 is 6 * 3 -1 19 is 6 x 3 +1 ... and so on. I don't know how far the pattern continues (past 100, I think), but you might want to play with it some and see how it holds up in general. It may be a quicker way to generate a list of primes than using the Sieve which I've seen you implement often in the past. My ass is still kicked from my last doctor's visit and all, and I'm not up to coding on it at the moment, but I figured I'd share the observation in case it interested you. RE: Prime pattern 6 +/- 1 - bplus - 06-30-2022 (06-30-2022, 10:49 AM)SMcNeill Wrote: Here's something I noticed the other day, which I thought might interest you, since you've written a ton of programs regarding prime numbers -- most primes tend to be multiples of 6, +/- 1! Yes it is a small 6 wheel: 2*3, next one up is 30 wheel: 2*3*5, and next one up is 210 wheel: 2*3*5*7 Wheels speed up Prime Sieving because there is no IF checking and much fewer loops. Smallest wheel is 2: odd and even numbers. Euclid: Primes are Infinite because Wheel + 1 is not divisible to all previous known primes (but not necessarily prime though very often is). RE: Prime pattern 6 +/- 1 - bplus - 06-30-2022 Here is Prime Sieving with a 6 wheeler: Code: (Select All) ' 6 wheel sieve.bas for QB64 fork (B+=MGA) 2017-08-30 copy and trans to QB64 PLUS: Once we have established primes, we can quickly factor numbers. RE: Prime pattern 6 +/- 1 - david_uwi - 06-30-2022 Here's one that I wrote for QB4.5 (amazingly you can go up to 100 million despite the memory restrictions). Code: (Select All) Rem finding primes to 100 million!!! RE: Prime pattern 6 +/- 1 - bplus - 06-30-2022 (06-30-2022, 12:46 PM)david_uwi Wrote: Here's one that I wrote for QB4.5 (amazingly you can go up to 100 million despite the memory restrictions). That's pretty fast @david_uwi, looks like a variation on a 30 wheeler, 1.5 secs on my system, mine takes 2.5 plus but mine can factor numbers once the sieving is complete. Shall we have a look at my 2310 wheeler? ought to do much better now that I am using 0.8.2 with optimized compiler. ;-)) RE: Prime pattern 6 +/- 1 - david_uwi - 06-30-2022 Of course that wouldn't work in qb4.5 (I have redimensioned the arrays) in qb4.5 you can only get to 2 million (array limitations) or use the hard disc as storage which is very slow. There is nothing clever in the program it just uses "the sieve" and stores the primes in a binary array for use in finding the next prime... RE: Prime pattern 6 +/- 1 - triggered - 07-01-2022 This brutal program looks at a different issue but reminds me of what's going on here. It turns out that every odd number can be produced by a formula a = (x * 2^y - 1) / 3 ... for some pair x, y (as long as x is indivisible by 3). This particular relation is used to help say things about the Collatz conjecture if anyone wants to reverse engineer it. For some reason, this code runs slower in later versions of QB64, faster in the SDL version, and fastest in QBjs. Not even close to ready when tried in the BAM. Code: (Select All) 'DefInt A-Z RE: Prime pattern 6 +/- 1 - SMcNeill - 07-01-2022 I sat and wrote one of these this morning, just to pass the time: Code: (Select All) $Checking:Off 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. RE: Prime pattern 6 +/- 1 - bplus - 07-01-2022 Quote:Are you certain that 20 second time is correct? For first 100,000,000 integers the 6 wheeler takes 2.5+ secs as posted in reply #6 to david_uwi in current QB64 v0.8.2 with compiler optimized. David's takes even less time, 1.5 secs on my system, but I don't think he is saving first factor information like I am to actually use the prime/composite array for something practical like factoring numbers. The 20 secs time was back in 2017 probably on my old dinosaur laptop and old version of QB64. If you are using the test: if pc mod p = 0 then pc is prime (prime candidate mod prime = 0 ie p divides pc), that is very slow test. RE: Prime pattern 6 +/- 1 - bplus - 07-01-2022 I just ran all 3 on my system and David's is winner (Windows 10 laptop, QB64 v 0.8.2 with compiler optimized) Steve, yours ran for 20+ secs by far the slowest time: Mine fell in middle 2.6+ secs this time: David's clear winner in speed contest of counting only: |