Sieve of Eratosthenes to find prime numbers

I have tried to implement the Sieve of Eratosthenes to find out the prime nos. in a given range of numbers but it has been having a runtime error . Here’s the code :
http://www.codechef.com/viewsolution/1967778

Please help me know the ambiguity .
Thank you.

segmentation fault is at line number 12. at line 12, value of i is m. but you cannot access list[m] as list is declared as an array of dimension m (0 to m-1).

1 Like

I have rectified the above problem and made some changes in the code . the algorithm still being the same, It’s still putting up a runtime error.Here’s the modified code:
http://www.codechef.com/viewsolution/1967942
…unable to figure out the segmentation violation in this code

rectified the above problem along with some changes in the code . the algorithm still being the same, It’s still putting up a runtime error.Here’s the modified code: http://www.codechef.com/viewsolution/1967942 …unable to figure out the segmentation violation in this code

1 Like

@piyukr…you are declaring an array of size more than allowed inside the function…i.e. list[m+1] which is main cause behind the SIGSEGV error.if you want to declare a big value like 10^6 size array then you should declare it out of the function i.e. globally.
but in the case if you declare a array of size 10^9 then it will not allowed…there you must rectify your problem for given domain .as clear by problem n-m=100000…this is the main key.

2 Likes

you second solution is giving runtime for inputs like 1 1000000 (for large values)…and your first solution is failing due to list[i] instead of list[j] in 12th line

@piyukr …you can implement by reading the code by @amitupadhyayhttp://www.codechef.com/viewsolution/1501682

2 Likes