19 / 19
Jun 2012

Hi all,

I was introduced to this site by a friend of mine.
I started with PRIME1 problem in C#

It worked with my IDE, ideone.com

However, when I submitted it to SPOJ, it returned with a runtime error SIGKILL.

Any suggestion?

You will have to post your code.

Runtime errors are input dependent. When you ran your code on ideone you did not use a test case that exploited your error. The judge tested with a case that caused your error.

That simply means my code is buggy still then?

using System;
using System.Text;
namespace SPOJ
{
    class Program
    {
        const int MAX_INPUT = 1000000000;
        const int MIN_INPUT = 1;
        const int MAX_DIFF = 100000;
        const int MAX_TEST = 10;
        const int MIN_TEST = 1;
        static void Main(string[] args)
    {
        string Input;
        string[] SeperateInput;
        int NumberOfTest;
        
        do
        {
            Console.Clear();
            Console.Write("Test case: ");
            NumberOfTest = int.Parse(Console.ReadLine());
        } while (NumberOfTest < MIN_TEST || NumberOfTest > MAX_TEST);

        int[] First = new int[NumberOfTest];
        int[] Second = new int[NumberOfTest];

        for (int i = 0; i < NumberOfTest; i++)
        {
            do
            {
                Console.Write("2 Numbers/ Case " + (i + 1) + ": ");
                Input = Console.ReadLine();
                SeperateInput = Input.Split(' ');

                if (SeperateInput.Length == 2)
                {
                    if (SeperateInput[0] != "" && 
                        SeperateInput[1] != "" && 
                       (SeperateInput[0] != SeperateInput[1]))
                    {
                        First[i] = int.Parse(SeperateInput[0]);
                        Second[i] = int.Parse(SeperateInput[1]);
                    }
                    else
                    {
                        Console.Clear();
                    }
                }
                else
                {
                    Console.Clear();
                }
            } while (!IsValidInPut(First[i], Second[i]));
        }

        for (int i = 0; i < NumberOfTest; i++)
        {
            Console.WriteLine();
            Console.WriteLine("Output: ");
            FindPrime(First[i], Second[i]);

        }
        Console.ReadLine();
    }

    static void FindPrime(int small, int big)
    {
        for (int i = 0; i <= big; i++)
        {
            bool IsPrime = true;

            for (int j = 2; j < i; j++)
            {
                if (i % j == 0)
                {
                    IsPrime = false;
                    break;
                }
            }

            if (IsPrime == true)
            {
                if (i >= small && i <= big)
                {
                    if (i != 1)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
        }
    }

    static bool IsValidInPut(int small, int big)
    {
        bool IsValid = true;

        int difference = big - small;

        if (big < small)
        {
            IsValid = false;
        }

        if (!(difference <= MAX_DIFF))
        {
            IsValid = false;
        }

        if (small < MIN_INPUT || big > MAX_INPUT)
        {
            IsValid = false;
        }

        if (small == big)
        {
            IsValid = false;
        }
        return IsValid;
    }
}
}

A: you don't need to verify that the input fits the input constraints. It is guaranteed that the input will match what the problem statement says.

B: Everything that print will be considered for the solution. Only print what the problem statement tells you to.

C: your findPrime function is incredibly slow. Test it out on the largest possible test case and ideone will quit on you too. It appears that the C# judge is returning SIGKILL instead of TLE in this case. You will need to find a more efficient algorithm to solve this problem.

I didnt know any printed would be considered that way.

Thank you for your response. I really appreciated that.

19 days later

My prime1 code :Sieve of Eratosthenes Algorithm

include

include

int main()
{
int t,m,n;
scanf("%d",&t);
for(int i=0;i{
scanf("%d %d",&m,&n);
int*array=new int [n];
for(int i=0;i{
array[i]=i+1;
}
for(int j=2;j<=n/2;j++)
{
for(int k=1;(j-1+j*k) {
array[j-1+j*k]=0;
}
}
for(int l=1;l{
if(array[l]>=m)
printf("%d\n",array[l]);
}
printf("\n");
}
return 0;
}
question

You cannot declare an array of size n. n is huge! Do you realize how much memory that is?!

....but memory allocation for array is dynamic and depend on users input..it will be deallocated automatically..

Then what's the use of free and delete ?

Vipul makes a good point.

My point however is that simply declaring an array of size 10^9 will exceed the memory of most computers today.

It uses the same amount of memory. You cannot declare an array that large. You need to fix your algorithm so that you don't need an array that large.

On SPOJ you can declare ( max memory usage allowed is 256MB ) :

Dynamic integer array of max size : 66422781

OR

Dynamic boolean array of max size : 265691124 =  66422781*4

at a time.

For any further memory usage you have to free the allocated memory.

Anyways, you need to improve your algorithm as suggested by leppy wink

thanks for help......I have modified my algo but now its getting TLE...... confused

include

bool isprime(int);
int main()
{
int t,m,n;
scanf("%d",&t);
for(int i=0;i{
scanf("%d %d",&m,&n);
for(int i=m;i<=n;i++)
{
if(i!=1)
{
if(isprime(i))
printf("%d\n",i);
}
}
printf("\n");
}

return 0;
}

bool isprime(int i)
{
for(int j=1;j<=i/2;j++)
{
if(j!=1){
if(i%j==0)
{return false;
break;}
}
}
return true;
} question

What do you think is the worst possible test case for your code based on the input constraints in the problem statement? Have you tested it?

i have reduced array size but still its not getting accepted........is my algo wrong????????????

yipeeeeeeeeee........ smiley ....it's done...........but still it took 2.78 sec.. and the best is 0.00 sec...how??? neutral_face