TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: KURT KUZBA
from: Jasen Betts
date: 1998-08-11 05:41:00
subject: Prime numbers

KK>    Your code has a few flaws and I couldn't find command.com
KK>    when I exited from running it in the IDE. I made a few more
KK>    changes. You should get 3,513 primes less than 32768.

ooh, it worked fine here, no memory mangling, (I got 3513)
oops! i may have edited it a bit before posting it,
here's a known good version.
seems slightly faster than your incs[inc] version.
see what I do with cap and limit to save on calculations and array
references, when I put that code into your routine it's nearly as fast as this
one.

It's a mess but i don't dare try to fix it for fear of breaking it.
It's still 5 times slower than a correctly applied Eratosthenes Sieve.

one of these days I'll do a piecewise sieve to go all the way to ULONG_MAX
a prototype found (but didn't print) over 24 000 000 primes in under 7 hours
(then I woke up) it was all 16 bit real mode (8088) code. using about 96k of
data I'll post that one later (when it's faster).

 /*_|_|   PRIMES.C
 _|_|_|   A program to find all the primes < 32,767.
 _|_|_|   No warrantee or guarantee is given or implied.
 _|_|_|   Released   PUBLIC DOMAIN   by Kurt Kuzba.  (10/2/96)*/
 _|_|_|   'upstep' and 'nosqrt' patches by Jasen Betts 5-Aug-98
#include 
#include 
#include 
int main(int c, char **v)
{
   register int current,*primes;
   int cap=9;
   int ndx=2,prime,check ,limit=3, show = 0,upstep=4;
   float runtime;
   primes = malloc(3600 * sizeof(int));
   if(primes == NULL)
     return puts("Malloc failure");
   primes[0] = 1, primes[1] = 2, primes[2] = 3;
   if(c > 1 && (v[1][0] == 's' || v[1][0] == 'S')) show++;

   for(current = 5; current < 32767; current +=(upstep^=6))
   {
      prime=check = 2;
      if ((current ) >= cap)
      {
        limit++;
        cap=primes[limit]*primes[limit];
      }
      while(prime && ++check < limit)
        prime = (current % primes[check]);

      if(prime)
        if(show)
          printf("%6d ", primes[++ndx] = current);
        else
          primes[++ndx] = current;

   }
   printf("\n%d Primes found in %.2f seconds", ndx + 1,
          clock()/(float)CLK_TCK);
   return 0;
}

--- EzyQwk V1.20 01fa018d
* Origin: CSS Brisbane, Qld, Australia. [61-7-3367-3890] (3:640/350)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728 810
SEEN-BY: 639/252 670/213 218
@PATH: 640/350 201 270/101 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.