Advertisement
Guest User

SPOJ Problem#2

a guest
Oct 23rd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace task2
  8. {
  9.     class pn
  10.     {
  11.         static public int[] PrimeNumbers(int m, int n)
  12.         {
  13.             int size=0;
  14.             int[] result = new int[size];
  15.             if (m==1)  {m=2;}
  16.             if (n==1)  {n=2;}
  17.             for (int i = m; i <= n; i++)
  18.             {
  19.                 int testprime=0;
  20.                 for (int j = 2; j < i; j++)
  21.                 {
  22.                     if (i % j == 0)
  23.                     { testprime++; }
  24.                 }
  25.                 if (testprime == 0)
  26.                 {
  27.                     size++;
  28.                     Array.Resize<int>(ref result, size);
  29.                     result[size - 1] = i;
  30.                 }
  31.             }
  32.             int LPN = result.Length;    
  33.             return result;
  34.         }
  35.     }
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.                  int t = Int32.Parse(Console.ReadLine());
  41.                  int[][] mn=new int[t][];
  42.                  for (int i = 0; i < t; i++)
  43.                  {
  44.                      string line = Console.ReadLine();
  45.                      mn[i] = line.Split(' ').Select(s => { return s == "" ? 0 : Convert.ToInt32(s); }).ToArray();
  46.                  }
  47.                  for (int i = 0; i < t; i++)
  48.                  {
  49.                      if (mn[i][0]==1 && mn[i][1]==1)
  50.                      {  
  51.                          Console.WriteLine("");
  52.                          break;
  53.                      }
  54.                      int lenght=pn.PrimeNumbers(mn[i][0], mn[i][1]).Length;
  55.                      int[] PNresult=new int[lenght];
  56.                      Array.Copy(pn.PrimeNumbers(mn[i][0], mn[i][1]), PNresult, lenght);
  57.                      for (int j = 0; j < lenght;j++)
  58.                      {
  59.                          Console.WriteLine("{0}", PNresult[j]);
  60.                      }
  61.                      Console.WriteLine("");
  62.                  }
  63.                  Console.ReadLine();
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement