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

×

Prime1:spoj

My code is giving false value and is giving false values.

  #include<stdio.h>
  #include<stdlib.h>
  int main()
  {
int t,*prime;
long long int m,n,i,j;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%lld%lld",&m,&n);
prime=(int*)(malloc((n*m)*sizeof(int)));

for(i=m;i<=n;i++)
 {
  prime[i]=1;
 }
prime[0]=0;
prime[1]=0;
for(i=m;i<=n;i++)
   {
   if(prime[i]!=0){
        for(j=2;j*i<=n;j++)
        {
        prime[j*i]=0;
        }
   }
   }
 for(i=m;i<=n;i++)
 {
     printf("%lld\n",i);
 }
free(prime);
m=0;n=0;
}

return 0; }

asked 02 Jan '15, 20:19

artist's gravatar image

1★artist
23214
accept rate: 0%

edited 02 Jan '15, 20:21


Your implementation of sieve is not correct. Refer here for the very basic and correct implementation of sieve.

Also, The range is large, this will not work. Allocation of such large memory is not allowed. Refer this and this.

link

answered 02 Jan '15, 20:42

damn_me's gravatar image

3★damn_me
2.6k21336
accept rate: 24%

edited 02 Jan '15, 20:51

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,477
×1,404
×1,136
×301

question asked: 02 Jan '15, 20:19

question was seen: 786 times

last updated: 02 Jan '15, 20:51