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

KK> JB>   to avoid the sqrt() you could if you're working
KK>    I did one using an integer based square root function which

I removed your sqrt (replaced with cap and limit) and experienced a
significant increase in speed. (your 486sx25 should be as fast as your
pentium was!)

Fwiw my "sieve of eratesthanes" is about 4 times faster than this code.
(same task in under 53 miliseconds on my 486) see my other post.

FWIW I'm running a 486sx25 at 33Mhz (or yours is doing 20) it was like that
when I got it, seems stable, (or mis-labeled), or possibly your compiler
produces less efficient executables than turbo-c 2.0 :)

your code with tweaks: (upstep  -  I always wanted an excuse to use ^= )

BTW check the documentation on clock(), it starts at 0 when the prog begins.

 /*_|_|   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)*/
#include 
#include 
#include 
int main(int c, char **v)
{
   int current, cap=9, *primes;
   int ndx=2,prime,check ,limit=3, show = 0,upstep=4;
   primes = malloc(3600 * sizeof(long 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];
        continue;  /* we know it's not a prime */
      }
      while(prime && ++check < limit)
      prime = (current % primes[check]);
      if(prime)
         if(show)
            printf("%8d", 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
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™.