Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > sci.electronics.design > #408742
| From | Jasen Betts <jasen@xnet.co.nz> |
|---|---|
| Newsgroups | sci.electronics.design |
| Subject | Re: anyone take a shot at this |
| Date | 2016-04-23 08:25 +0000 |
| Organization | "JJ's own news server" |
| Message-ID | <nffbhc$roa$1@gonzo.alcatraz> (permalink) |
| References | (1 earlier) <67fb3d1b-2b65-4532-bea3-bdd282dc8f66@googlegroups.com> <4c828146-2448-4d29-8097-f01ab6577285@googlegroups.com> <bfcaefd1-c7d6-4889-b12f-0c2408ef373e@googlegroups.com> <nfbp12$b0q$1@gonzo.alcatraz> <9023ec34-fa71-498d-9254-69ca2ff88e42@googlegroups.com> |
On 2016-04-22, mrdarrett@gmail.com <mrdarrett@gmail.com> wrote:
> On Thursday, April 21, 2016 at 5:31:42 PM UTC-7, Jasen Betts wrote:
>> >
>> > Oh, I don't know. His post inspired me to look at the Sieve of
>> Eratosthenes. Quite fun. There's a sample sieve code with the
>> FreeBasic compiler. After some modification (it only finds the first
>> million primes) it took my computer just 7.5 seconds to find the first
>> 25 million primes. I'd forgotten how much fun such things could be.
>> Of course, to find these primes, about 2 gigabytes of RAM for array
>> space are needed... I'm sure the algorithm could be refined further by
>> avoiding storage of multiples of two...
>>
>> sounds pretty crap if it runs out of memorty like that.
>> I was able to avoid that problem in 1998 (found 200 million primes
>> used less than 128K memory)
>>
>> --
>> \_(ツ)_
>
>
> Which algorithm did you use
piecewise seive.
basically the same algorithm, but recycling the seive memory.
> and how long did it take?
"all night" on a 486sx 33Mhz running DOS
> Eratosthenes is supposed to be fast, something like log log N.
AFAIK it's the fastest way to find all primes.
https://fidonet.ozzmosis.com/echomail.php/c_echo/8333893e42ae1d27.html
I think that syntax error is noise (delete that line)
It doesn't run right on 64 bit systems, I think because it makes unwise
assumptions on the magnitude if ULONG_MAX.
compiled for 64 bit linux it took about 37 seconds to search to 2^32
on my work PC then failed to stop and eventually died with segv, my
laptop is a bit slower. it took 4 minutes.
in 1998 it would have been faster to read 200 million primes from a
CD-rom
attached is a slightly updated version, I haven't tested it past 2^32
/*_|_| erastothenes.c
_|_|_| A program to find all the primes <= MAX_ULONG
_|_|_| No warrantee or guarantee implied.
_|_|_| Released PUBLIC DOMAIN by Jasen Betts. (11/Aug/98)*/
/* updated for more modren systems 2016 */
/* here's a piecewise implememtation of erastothenes' sieve for
* finding prime numbers, it should be able to go all the way
* to MAX_ULONG, (see <limits.h>).
* because it takes some time to run it prints a status line
* (to stderr) every 1000 primes, this behavior can be modified
* by defining STATUS_FREQ to another number, 0 disables it
*
* SIEVESIZE determines the size of the char-array used to do the sieveing
* factors of (MAX_ULONG+1) should be used (they are all powers of 2)
* 32768u is the biggest my 16-bit compiler allows.
*/
#include <stdio.h>
#include <math.h> /* only used for sqrt() */
#include <time.h>
#include <stdlib.h>
#define SIEVESIZE (32768u) /* must be a power of 2 */
#define SIEVEMAX (SIEVESIZE-1)
#ifndef STATUS_FREQ
#define STATUS_FREQ (1000000)
#endif
/*when to stop (should be a multiple of seivesize
* else it will overshoot a little)
*/
#define MAXPRIME (0x100000000ll)
/* tablesize should be more than the number of
* primes under sqrt(MAXPRIME)
* 7000 is enough to compute upto 2^32
* if you set it too low you will
* get undefined behaviour.
* To go to 2^64 use 204000000 here
*/
#define TABLESIZE 7000
char sieve[SIEVESIZE];
unsigned long x,y,z,count=1,max,base=0,roof;
unsigned long t;
unsigned i,j;
int print,hits=STATUS_FREQ-1;
int main(int argc,char**argv){
unsigned long *primes,*tuples;
primes=calloc(sizeof(long),TABLESIZE);
if (NULL==primes){puts("MALLOC ERROR-1");abort();}
tuples=calloc(sizeof(long),TABLESIZE);
if (NULL==tuples){puts("MALLOC ERROR-2");abort();}
primes[0]=2;tuples[0]=4;
primes[1]=3;tuples[1]=9;
print=(argc > 1);
for(i=0;i<SIEVEMAX;) {sieve[i++]=0;sieve[i++]=1;}
max = (int) sqrt(SIEVEMAX)+1;
for(i=3; primes[(int)count-1]<=max;i++)
if(sieve[i]){
primes[(int)count]=i+base;
j=(int)(tuples[(int)count]=(base+i)*(i+base));
while(j<max)
{
sieve[j-(int)base]=0;
j+=(int)primes[(int)count]*2;
}
++count;
}
count=-1;
base=0;
do
{
roof=base+SIEVESIZE;
max = (long)floor(sqrt(roof-1)+1) ;
// if(roof>100000) #roof-=65536;
for(i=0;i<SIEVESIZE;) {sieve[i++]=0;sieve[i++]=1;}
for(i=1;(x=primes[i])<=max;++i)
{
y=tuples[i];
x<<=1;
while((long)(roof-y) >0) /* coded like that to foil integer overflow */
{
sieve[(int)(y-base)]=0;
y+=x;
}
tuples[i]=y;
}
for(i=0;i<SIEVESIZE;i++)
if(sieve[i]){
if((i+base)==1l)i++;
if (print) printf("%6ld\n",i+base);
if (++count<7000)
{
primes[(int)(count)]=i+base;
if(!tuples[(int)count]) tuples[(int)count]=(base+i)*(i+base);
}
#if STATUS_FREQ
if(!--hits)
{
fprintf(stderr,"\rcount: %12ld / prime: %13lu time: %8.1f"
,count+2, i+base, (double)clock()/CLOCKS_PER_SEC);
hits=STATUS_FREQ;
}
#endif
}
base +=SIEVESIZE;
}
while( base && base < MAXPRIME );
printf("\n%lu primes %.1f seconds\n",count+2,(double)clock()/CLOCKS_PER_SEC);
return 0;
}
--
\_(ツ)_
Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
anyone take a shot at this Jamie M <jmorken@shaw.ca> - 2016-04-19 14:53 -0700
Re: anyone take a shot at this CORRECTED Jamie M <jmorken@shaw.ca> - 2016-04-19 14:58 -0700
Re: anyone take a shot at this DecadentLinuxUserNumeroUno <DLU1@DecadentLinuxUser.org> - 2016-04-19 22:39 -0400
Re: anyone take a shot at this bitrex <bitrex@de.lete.earthlink.net> - 2016-04-20 14:02 -0400
Re: anyone take a shot at this John S <Sophi.2@invalid.org> - 2016-04-20 13:13 -0500
Re: anyone take a shot at this DecadentLinuxUserNumeroUno <DLU1@DecadentLinuxUser.org> - 2016-04-20 16:34 -0400
Re: anyone take a shot at this DecadentLinuxUserNumeroUno <DLU1@DecadentLinuxUser.org> - 2016-04-20 16:31 -0400
Re: anyone take a shot at this bitrex <bitrex@de.lete.earthlink.net> - 2016-04-20 18:32 -0400
Re: anyone take a shot at this DecadentLinuxUserNumeroUno <DLU1@DecadentLinuxUser.org> - 2016-04-20 20:50 -0400
Re: anyone take a shot at this mrdarrett@gmail.com - 2016-04-20 14:04 -0700
Re: anyone take a shot at this bill.sloman@ieee.org - 2016-04-20 23:36 -0700
Re: anyone take a shot at this mrdarrett@gmail.com - 2016-04-21 11:03 -0700
Re: anyone take a shot at this Jamie M <jmorken@shaw.ca> - 2016-04-21 16:57 -0700
Re: anyone take a shot at this Jasen Betts <jasen@xnet.co.nz> - 2016-04-21 23:50 +0000
Re: anyone take a shot at this mrdarrett@gmail.com - 2016-04-22 15:16 -0700
Re: anyone take a shot at this Jasen Betts <jasen@xnet.co.nz> - 2016-04-23 08:25 +0000
Re: anyone take a shot at this mrdarrett@gmail.com - 2016-04-23 15:36 -0700
Re: anyone take a shot at this Jasen Betts <jasen@xnet.co.nz> - 2016-04-24 01:24 +0000
Re: anyone take a shot at this Bill Sloman <bill.sloman@ieee.org> - 2016-04-24 13:31 +1000
Re: anyone take a shot at this bill.sloman@ieee.org - 2016-04-23 20:35 -0700
Re: anyone take a shot at this mrdarrett@gmail.com - 2016-04-23 21:26 -0700
Re: anyone take a shot at this bill.sloman@ieee.org - 2016-04-24 00:06 -0700
Re: anyone take a shot at this Jasen Betts <jasen@xnet.co.nz> - 2016-04-24 12:05 +0000
Re: anyone take a shot at this bloggs.fredbloggs.fred@gmail.com - 2016-04-20 18:18 -0700
Re: anyone take a shot at this Jamie M <jmorken@shaw.ca> - 2016-04-21 00:35 -0700
Re: anyone take a shot at this bill.sloman@ieee.org - 2016-04-21 00:44 -0700
csiph-web