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;
}
}
}