| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Prime numbers |
JB> I removed your sqrt
Good idea.
JB> your 486sx25 should be as fast as your pentium was!
Not quite... but better. :)
JB> less efficient executables than turbo-c 2.0 :)
QC2.5. It only does 286 optimizations, not 386.
JB> upstep - I always wanted an excuse to use ^=
Good idea, but I went with an array in steps of 30.
JB> clock(), it starts at 0 when the prog begins
I know, but I don't always have the timing loop as the
whole program, so I habitually use variables. QC2.5 already
gives program execution time at program exit. They match. :)
Your code has a few flaws and I couldn't find command.com
when I exited from running it in the IDE. I made a few more
changes. You should get 3,513 primes less than 32768.
This runs in about half a second when compiled under either
QC2.5 ( 286 optimizations ) or BC++3.1 ( 386 optimizations )
on a 486sx25 ( with show option disabled on the command line )
and in about .05 seconds on a Pentium 166 system.
/*_|_| PRIMES.C
_|_|_| A program to find all signed integer primes.
_|_|_| No warrantee or guarantee is given or implied.
_|_|_| Released PUBLIC DOMAIN by Kurt Kuzba. (8/6/98)*/
#include
#include
#include
int main(int c, char **v)
{
int ndx, check, prime, current, *primes, inc, root, show, ret;
int incs[8] = { 2, 6, 4, 2, 4, 2, 4, 6 };
float runtime = (float)clock();
primes = malloc(32000 * sizeof(int));
if(primes == NULL)
return puts("Malloc failure");
root = show = ret = 0;
inc = 7;
ndx = 9;
primes[0] = 1, primes[1] = 2, primes[2] = 3, primes[3] = 5,
primes[4] = 7, primes[5] = 11, primes[6] = 13,
primes[7] = 17, primes[8] = 19, primes[9] = 23;
if(c > 1 && (v[1][0] == 's' || v[1][0] == 'S')) show++;
for(current = 29; current > 0; current += incs[inc] )
{
inc = (inc + 1) % 8;
check = prime = 1;
while((long)current > ((long)root * (long)root))
root++;
while(prime && primes[++check] <= root)
prime = current % primes[check];
if(prime)
if(show)
printf("%7d%s",
primes[++ndx] = current, !((++ret) % 10) ? "\n" :
"");
else
primes[++ndx] = current;
}
runtime = ((float)clock() - runtime) / CLK_TCK;
printf("\n%d Primes found in %.2f seconds", ndx + 1, runtime);
return 0;
}
/*_|_| end PRIMES.C */
or... if you dislike white space...
/**/
/**/
/**/
/**/
/**************/ /*********************/
#include /** Fire Torpedos!! **/
#include /*********************/
#include /**/
int main(int c,char **v){int x,k,p,z,*r,i,o,w,e,n[8]={2,6,4,2,4,
2,4,6};float rt=(float)clock();r=malloc(32000*sizeof(int));if(NULL
==r)return puts("Malloc failure");o=w=e=0;i=7;x=9;r[0]=1,r[1]=2,r[2]
=3,r[3]=5,r[4]=7,r[5]=11,r[6]=13,r[7]=17,r[8]=19,r[9]=23;if(z>1&&(
v[1][0]=='s'||v[1][0]=='S'))w++;for(z=29;z>0;z+=n[i]){i=(i+1)%8;
k=p=1;while((long)z>((long)o*(long)o))o++;while(p&&r[++k]<=o)
p=z%r[k];if(p)if(w){printf("%7d%s",r[++x]=z,!((++e)%10)?"\n":
"");}else r[++x]=z;}rt=((float)clock()-rt)/CLK_TCK;printf( /*(====)*/
"\n%d Primes found in %.2f seconds",x+1,rt);return 0;}
> ] * Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)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: 154/750 222 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™.