PrateekLovesCandy Run Error

Why my code i giving me Run Error in Compile Message when i submit the code. Although it gives no error During Compile & Test and Compile’s Successfully. Also code run’s Fine on my machine. Please Help !
CodingBlocks IDE link(My Code) : - https://ide.codingblocks.com/s/464804

see the constraint on number of test cases, It’s quite large because for every test case you are going to find the prime numbers upto that number and at last return the last prime number found, this will cause the TLE for big test cases.

  • So, to overcome this problem, let’s just store your test cases in an array.
  • Then find the maximum number from all of the testcases.
  • then, Use SOE(Sieve of Eratosthenes) to find the prime number upto that number and store that an the array.
  • After storing, simply loop over every test case that was stored previously in an array.
  • Print the value of n from the array storing primes.

That’s how we need not to find the prime number for every test case

I have Done Exactly how you have told to solve but rather finding prime number upto a certain number(i.e Maximum number in test case ) i have taken a fixed number(i.e 10001) and finded the prime number upto 10001 and then i print the prime number upto the maximum number in the test case … now on compile and test it compile successfully but on submission it gives an run error . Please help and tell me where i am going wrong

The 500,000th prime is 7,368,787

so make size of p array at least 7million
ideally it should be not give segmentation fault for size 7400000, but if it is giving then try to
make the array global

I increased the size prime array to 7 million and also changed the every integer variable to long …this helped me avoid Run Error but now it only passed 4 test cases and only test case 0 is not passed which is giving me TLE error now Coding block’s IDE link : https://ide.codingblocks.com/s/466004

u need to optimise ur approach.

store all prime numbers in some arraylist like
ArrayList primes = sieve(max);

and then use that arraylist to answer each query in o(1)

Thanks @A__Ag this approach worked and i was able to pass all the test cases :smile:

1 Like