You are not logged in. Please login at www.codechef.com to post your questions!

×

faster prime generating with seive

I use a seive code for generating primes.I dont know if it is fast enough.If anyone have any idea to make my code faster then please help.Or give me a faster code so I can store that for later use.

Thanks in advance.

//function sieve begin:

#define Max 20000000
int prime_int[Max];
bool prime_bool[Max];
void prime_num()
{
    int i,j,root=sqrt(Max);
    for(i=4;i<=Max;i+=2)
    prime_bool[i]=true;
    for(i=3;i<=root;i+=2)
    if(!prime_bool[i])
    {
        for(j=i*i;j<=Max;j+=2*i)
        prime_bool[j]=true;
    }
    prime_int[0]=2;
    for(i=3,j=1;i<Max;i+=2)
    if(!prime_bool[i])prime_int[j++]=i;
}

//function sieve end

asked 25 Nov '14, 21:56

moudud99's gravatar image

4★moudud99
1861513
accept rate: 11%


here is a fastest method to generate primes.. other methods are sieve of eratosthenes.
the second method is easier to understand and implement also :)

link

answered 25 Nov '14, 22:15

rjohari23's gravatar image

3★rjohari23
779214
accept rate: 14%

That code is implement of sieve of eratosthenes

(25 Nov '14, 22:29) moudud994★
link

answered 26 Nov '14, 00:23

rishabhprsd7's gravatar image

2★rishabhprsd7
1.9k11243
accept rate: 14%

well I can not do python but that list helps.Thanks.

(26 Nov '14, 10:41) moudud994★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,917
×1,664
×1,490
×23

question asked: 25 Nov '14, 21:56

question was seen: 800 times

last updated: 26 Nov '14, 10:41