1 / 2
Mar 2017

Hi,
I have been trying to solve problem Prime1 with C#. It has been less than a week since I started the language.
I keep getting Runtime error (NZEC) for my submissions. Here is my current code:

using System;
using System.Collections.Generic;
namespace PrimeGenerator
{
class Program
{
static void Main(string[] args)
{

        Console.WriteLine("Please enter two numbers");
        string str = Console.ReadLine();
        string[] nums = str.Split(default(string[]), 3, StringSplitOptions.RemoveEmptyEntries);
        int n = Int32.Parse(nums[0]);
        int m = Int32.Parse(nums[1]);
        var d = sieve(n,m);
        for(var i = 0; i<d.Count;i++)
        {
          Console.WriteLine(d[i]);
        }

        Console.ReadLine();
    }
    public static bool isPrime(int p)
    {
        for(var i = 2;i<=Math.Sqrt(p);i++)
        {
          if(p%i==0)
          {
            return false;
          }
          if(i>2){
            continue;
          }
        }
        return true;
    }
    public static List<int> sieve(int r, int q)
    {
      List<int> primes = new List<int>();
      for(var i = r; i <= q;i++)
      {
        if(i>7 && (i%2 == 0 || i%5==0||i%7==0)){
          continue;
        }
        if(isPrime(i))
        {
          primes.Add(i);
        }
      }
      return primes;
    }
}

}

  • created

    Mar '17
  • last reply

    Mar '17
  • 1

    reply

  • 1.2k

    views

  • 2

    users

The problem statement says: "The input begins with the number t of test cases in a single line (t<=10). "
Where do your read and process this number?

Furthermore you have to avoid this Console.WriteLine("Please enter two numbers"); because the judge will consider any output when checking you program.